summaryrefslogtreecommitdiff
path: root/Widgets
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-01-11 04:40:05 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-01-11 04:40:05 +0100
commitb95aa1831529d7cbd9cdfa05797836bf5fce21be (patch)
tree77f9dc503258188597b6ed161453ccf8d4686027 /Widgets
parenta3c39ea9d6782b69196eb623cdc2d30276e0f1fa (diff)
downloadserenity-b95aa1831529d7cbd9cdfa05797836bf5fce21be.zip
Hook up the Keyboard device to the AbstractScreen.
Basic text editing in a TextBox works. How very cool :^)
Diffstat (limited to 'Widgets')
-rw-r--r--Widgets/AbstractScreen.cpp55
-rw-r--r--Widgets/AbstractScreen.h6
-rw-r--r--Widgets/Event.h1
3 files changed, 61 insertions, 1 deletions
diff --git a/Widgets/AbstractScreen.cpp b/Widgets/AbstractScreen.cpp
index d77318ef14..f3da849f50 100644
--- a/Widgets/AbstractScreen.cpp
+++ b/Widgets/AbstractScreen.cpp
@@ -29,6 +29,7 @@ AbstractScreen::AbstractScreen(unsigned width, unsigned height)
m_cursor_location = rect().center();
PS2MouseDevice::the().set_client(this);
+ Keyboard::the().set_client(this);
}
AbstractScreen::~AbstractScreen()
@@ -62,3 +63,57 @@ void AbstractScreen::did_receive_mouse_data(int dx, int dy, bool left_button, bo
if (m_cursor_location != prev_location)
WindowManager::the().redraw_cursor();
}
+
+void AbstractScreen::on_key_pressed(Keyboard::Key key)
+{
+ auto event = make<KeyEvent>(Event::KeyDown, 0);
+ int key_code = 0;
+
+ switch (key.character) {
+ case 8: key_code = KeyboardKey::Backspace; break;
+ case 10: key_code = KeyboardKey::Return; break;
+ }
+ event->m_key = key_code;
+
+ if (key.character) {
+ char buf[] = { 0, 0 };
+ char& ch = buf[0];
+ ch = key.character;
+ if (key.shift()) {
+ if (ch >= 'a' && ch <= 'z') {
+ ch &= ~0x20;
+ } else {
+ switch (ch) {
+ case '1': ch = '!'; break;
+ case '2': ch = '@'; break;
+ case '3': ch = '#'; break;
+ case '4': ch = '$'; break;
+ case '5': ch = '%'; break;
+ case '6': ch = '^'; break;
+ case '7': ch = '&'; break;
+ case '8': ch = '*'; break;
+ case '9': ch = '('; break;
+ case '0': ch = ')'; break;
+ case '-': ch = '_'; break;
+ case '=': ch = '+'; break;
+ case '`': ch = '~'; break;
+ case ',': ch = '<'; break;
+ case '.': ch = '>'; break;
+ case '/': ch = '?'; break;
+ case '[': ch = '{'; break;
+ case ']': ch = '}'; break;
+ case '\\': ch = '|'; break;
+ case '\'': ch = '"'; break;
+ case ';': ch = ':'; break;
+ }
+ }
+ }
+ event->m_text = buf;
+ }
+
+ event->m_shift = key.shift();
+ event->m_ctrl = key.ctrl();
+ event->m_alt = key.alt();
+
+ EventLoop::main().postEvent(&WindowManager::the(), move(event));
+}
diff --git a/Widgets/AbstractScreen.h b/Widgets/AbstractScreen.h
index 752a5d34ae..cfbe456000 100644
--- a/Widgets/AbstractScreen.h
+++ b/Widgets/AbstractScreen.h
@@ -3,9 +3,10 @@
#include "Object.h"
#include "Rect.h"
#include "Size.h"
+#include "Keyboard.h"
#include "PS2MouseDevice.h"
-class AbstractScreen : public Object, public MouseClient {
+class AbstractScreen : public Object, public KeyboardClient, public MouseClient {
public:
virtual ~AbstractScreen();
@@ -30,6 +31,9 @@ private:
// ^MouseClient
virtual void did_receive_mouse_data(int dx, int dy, bool left_button, bool right_button) final;
+ // ^KeyboardClient
+ virtual void on_key_pressed(Keyboard::Key) final;
+
int m_width { 0 };
int m_height { 0 };
diff --git a/Widgets/Event.h b/Widgets/Event.h
index 7178753e9b..a1ebb45c88 100644
--- a/Widgets/Event.h
+++ b/Widgets/Event.h
@@ -136,6 +136,7 @@ public:
private:
friend class EventLoop;
+ friend class AbstractScreen;
int m_key { 0 };
bool m_ctrl { false };
bool m_alt { false };