editor, keyboard: Add support for arrow keys
authorJan Arne Petersen <jpetersen@openismus.com>
Mon, 17 Sep 2012 13:28:07 +0000 (15:28 +0200)
committerKristian Høgsberg <krh@bitplanet.net>
Tue, 25 Sep 2012 15:24:49 +0000 (11:24 -0400)
Add support for arrow keys on the virtual keyboard and make it possible
to move around the cursor in the editor example.

clients/editor.c
clients/keyboard.c

index 301cbe2..5ad1aa2 100644 (file)
@@ -288,13 +288,12 @@ text_model_key(void *data,
                uint32_t key,
                uint32_t state)
 {
+       struct text_entry *entry = data;
        const char *state_label;
-       const char *key_label;
+       const char *key_label = "released";
 
        if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
                state_label = "pressed";
-       } else {
-               state_label = "released";
        }
 
        switch (key) {
@@ -304,6 +303,20 @@ text_model_key(void *data,
                case XKB_KEY_KP_Enter:
                        key_label = "Enter";
                        break;
+               case XKB_KEY_Left:
+                       if (entry->cursor > 0) {
+                               entry->cursor--;
+                               entry->anchor = entry->cursor;
+                               widget_schedule_redraw(entry->widget);
+                       }
+                       break;
+               case XKB_KEY_Right:
+                       if (entry->cursor < strlen(entry->text)) {
+                               entry->cursor++;
+                               entry->anchor = entry->cursor;
+                               widget_schedule_redraw(entry->widget);
+                       }
+                       break;
                default:
                        key_label = "Unknown";
        }
index c90c5b2..bf0e9f0 100644 (file)
@@ -47,7 +47,11 @@ enum key_type {
        keytype_space,
        keytype_switch,
        keytype_symbols,
-       keytype_tab
+       keytype_tab,
+       keytype_arrow_up,
+       keytype_arrow_left,
+       keytype_arrow_right,
+       keytype_arrow_down
 };
 
 struct key {
@@ -96,9 +100,13 @@ static const struct key keys[] = {
        { keytype_default, ".", ".", 1},
        { keytype_switch, "ABC", "abc", 1},
 
-       { keytype_symbols, "?123", "?123", 2},
-       { keytype_space, "", "", 8},
-       { keytype_symbols, "?123", "?123", 2}
+       { keytype_symbols, "?123", "?123", 1},
+       { keytype_space, "", "", 6},
+       { keytype_arrow_up, "/\\", "/\\", 1},
+       { keytype_arrow_left, "<", "<", 1},
+       { keytype_arrow_right, ">", ">", 1},
+       { keytype_arrow_down, "\\/", "\\/", 1},
+       { keytype_symbols, "?123", "?123", 1}
 };
 
 static const unsigned int columns = 12;
@@ -255,6 +263,22 @@ keyboard_handle_key(struct keyboard *keyboard, const struct key *key)
                        input_method_context_key(keyboard->keyboard->context,
                                                 XKB_KEY_Tab, WL_KEYBOARD_KEY_STATE_PRESSED);
                        break;
+               case keytype_arrow_up:
+                       input_method_context_key(keyboard->keyboard->context,
+                                                XKB_KEY_Up, WL_KEYBOARD_KEY_STATE_PRESSED);
+                       break;
+               case keytype_arrow_left:
+                       input_method_context_key(keyboard->keyboard->context,
+                                                XKB_KEY_Left, WL_KEYBOARD_KEY_STATE_PRESSED);
+                       break;
+               case keytype_arrow_right:
+                       input_method_context_key(keyboard->keyboard->context,
+                                                XKB_KEY_Right, WL_KEYBOARD_KEY_STATE_PRESSED);
+                       break;
+               case keytype_arrow_down:
+                       input_method_context_key(keyboard->keyboard->context,
+                                                XKB_KEY_Down, WL_KEYBOARD_KEY_STATE_PRESSED);
+                       break;
        }
 }