From ef4f36dea3a3bdc48702b292982b58f2d0990cf4 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Mon, 19 Oct 2015 12:24:44 +0300 Subject: [PATCH 1/2] Added special multimedia keycodes + restructured code --- src/keycode.h | 69 +++++++++++++++- src/robotjs.cc | 208 ++++++++++++++++++------------------------------- 2 files changed, 143 insertions(+), 134 deletions(-) diff --git a/src/keycode.h b/src/keycode.h index ee499e51..5d1da602 100644 --- a/src/keycode.h +++ b/src/keycode.h @@ -15,6 +15,7 @@ extern "C" #include enum _MMKeyCode { + K_NOT_A_KEY = -1, K_BACKSPACE = kVK_Delete, K_DELETE = kVK_ForwardDelete, K_RETURN = kVK_Return, @@ -45,7 +46,27 @@ enum _MMKeyCode { K_CONTROL = kVK_Control, K_SHIFT = kVK_Shift, K_CAPSLOCK = kVK_CapsLock, - K_SPACE = kVK_Space + K_SPACE = kVK_Space, + K_PRINTSCREEN = K_NOT_A_KEY, + + K_AUDIO_VOLUME_MUTE = kVK_Mute, + K_AUDIO_VOLUME_DOWN = kVK_VolumeDown, + K_AUDIO_VOLUME_UP = kVK_VolumeUp, + K_AUDIO_PLAY = K_NOT_A_KEY, + K_AUDIO_STOP = K_NOT_A_KEY, + K_AUDIO_PAUSE = K_NOT_A_KEY, + K_AUDIO_PREV = K_NOT_A_KEY, + K_AUDIO_NEXT = K_NOT_A_KEY, + K_AUDIO_REWIND = K_NOT_A_KEY, + K_AUDIO_FORWARD = K_NOT_A_KEY, + K_AUDIO_REPEAT = K_NOT_A_KEY, + K_AUDIO_RANDOM = K_NOT_A_KEY, + + K_LIGHTS_MON_UP = K_NOT_A_KEY, + K_LIGHTS_MON_DOWN = K_NOT_A_KEY, + K_LIGHTS_KBD_TOGGLE = K_NOT_A_KEY, + K_LIGHTS_KBD_UP = K_NOT_A_KEY, + K_LIGHTS_KBD_DOWN = K_NOT_A_KEY }; typedef CGKeyCode MMKeyCode; @@ -53,8 +74,10 @@ typedef CGKeyCode MMKeyCode; #elif defined(USE_X11) #include +#include enum _MMKeyCode { + K_NOT_A_KEY = -1, K_BACKSPACE = XK_BackSpace, K_DELETE = XK_Delete, K_RETURN = XK_Return, @@ -85,7 +108,27 @@ enum _MMKeyCode { K_CONTROL = XK_Control_L, K_SHIFT = XK_Shift_L, K_CAPSLOCK = XK_Shift_Lock, - K_SPACE = XK_space + K_SPACE = XK_space, + K_PRINTSCREEN = XK_Print, + + K_AUDIO_VOLUME_MUTE = XF86XK_AudioMute, + K_AUDIO_VOLUME_DOWN = XF86XK_AudioLowerVolume, + K_AUDIO_VOLUME_UP = XF86XK_AudioRaiseVolume, + K_AUDIO_PLAY = XF86XK_AudioPlay, + K_AUDIO_STOP = XF86XK_AudioStop, + K_AUDIO_PAUSE = XF86XK_AudioPause, + K_AUDIO_PREV = XF86XK_AudioPrev, + K_AUDIO_NEXT = XF86XK_AudioNext, + K_AUDIO_REWIND = XF86XK_AudioRewind, + K_AUDIO_FORWARD = XF86XK_AudioForward, + K_AUDIO_REPEAT = XF86XK_AudioRepeat, + K_AUDIO_RANDOM = XF86XK_AudioRandomPlay, + + K_LIGHTS_MON_UP = XF86XK_MonBrightnessUp, + K_LIGHTS_MON_DOWN = XF86XK_MonBrightnessDown, + K_LIGHTS_KBD_TOGGLE = XF86XK_KbdLightOnOff, + K_LIGHTS_KBD_UP = XF86XK_KbdBrightnessUp, + K_LIGHTS_KBD_DOWN = XF86XK_KbdBrightnessDown }; typedef KeySym MMKeyCode; @@ -93,6 +136,7 @@ typedef KeySym MMKeyCode; #elif defined(IS_WINDOWS) enum _MMKeyCode { + K_NOT_A_KEY = -1, K_BACKSPACE = VK_BACK, K_DELETE = VK_DELETE, K_RETURN = VK_RETURN, @@ -124,7 +168,26 @@ enum _MMKeyCode { K_ALT = VK_MENU, K_CAPSLOCK = VK_CAPITAL, K_SPACE = VK_SPACE, - K_PRINTSCREEN = VK_SNAPSHOT + K_PRINTSCREEN = VK_SNAPSHOT, + + K_AUDIO_VOLUME_MUTE = VK_VOLUME_MUTE, + K_AUDIO_VOLUME_DOWN = VK_VOLUME_DOWN, + K_AUDIO_VOLUME_UP = VK_VOLUME_UP, + K_AUDIO_PLAY = VK_MEDIA_PLAY_PAUSE, + K_AUDIO_STOP = VK_MEDIA_STOP, + K_AUDIO_PAUSE = VK_MEDIA_PLAY_PAUSE, + K_AUDIO_PREV = VK_MEDIA_PREV_TRACK, + K_AUDIO_NEXT = VK_MEDIA_NEXT_TRACK, + K_AUDIO_REWIND = K_NOT_A_KEY, + K_AUDIO_FORWARD = K_NOT_A_KEY, + K_AUDIO_REPEAT = K_NOT_A_KEY, + K_AUDIO_RANDOM = K_NOT_A_KEY, + + K_LIGHTS_MON_UP = K_NOT_A_KEY, + K_LIGHTS_MON_DOWN = K_NOT_A_KEY, + K_LIGHTS_KBD_TOGGLE = K_NOT_A_KEY, + K_LIGHTS_KBD_UP = K_NOT_A_KEY, + K_LIGHTS_KBD_DOWN = K_NOT_A_KEY }; typedef int MMKeyCode; diff --git a/src/robotjs.cc b/src/robotjs.cc index 12e7a2d3..1df4a562 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -243,145 +243,91 @@ NAN_METHOD(scrollMouse) |_|\_\___|\__, |_.__/ \___/ \__,_|_| \__,_| |___/ */ +struct KeyNames { + const char* name; + MMKeyCode key; +}; + +static KeyNames key_names[] = +{ + { "backspace", K_BACKSPACE }, + { "delete", K_DELETE }, + { "enter", K_RETURN }, + { "tab", K_TAB }, + { "escape", K_ESCAPE }, + { "up", K_UP }, + { "down", K_DOWN }, + { "right", K_RIGHT }, + { "left", K_LEFT }, + { "home", K_HOME }, + { "end", K_END }, + { "page_up", K_PAGEUP }, + { "page_down", K_PAGEDOWN }, + { "f1", K_F1 }, + { "f2", K_F2 }, + { "f3", K_F3 }, + { "f4", K_F4 }, + { "f5", K_F5 }, + { "f6", K_F6 }, + { "f7", K_F7 }, + { "f8", K_F8 }, + { "f9", K_F9 }, + { "f10", K_F10 }, + { "f11", K_F11 }, + { "f12", K_F12 }, + { "command", K_META }, + { "alt", K_ALT }, + { "control", K_CONTROL }, + { "shift", K_SHIFT }, + { "caps", K_CAPSLOCK }, + { "space", K_SPACE }, + { "printscreen", K_PRINTSCREEN }, + + { "audio_mute", K_AUDIO_VOLUME_MUTE }, + { "audio_vol_down", K_AUDIO_VOLUME_DOWN }, + { "audio_vol_up", K_AUDIO_VOLUME_UP }, + { "audio_play", K_AUDIO_PLAY }, + { "audio_stop", K_AUDIO_STOP }, + { "audio_pause", K_AUDIO_PAUSE }, + { "audio_prev", K_AUDIO_PREV }, + { "audio_next", K_AUDIO_NEXT }, + { "audio_rewind", K_AUDIO_REWIND }, + { "audio_forward", K_AUDIO_FORWARD }, + { "audio_repeat", K_AUDIO_REPEAT }, + { "audio_random", K_AUDIO_RANDOM }, + + { "lights_mon_up", K_LIGHTS_MON_UP }, + { "lights_mon_down", K_LIGHTS_MON_DOWN }, + { "lights_kbd_toggle",K_LIGHTS_KBD_TOGGLE }, + { "lights_kbd_up", K_LIGHTS_KBD_UP }, + { "lights_kbd_down", K_LIGHTS_KBD_DOWN }, + + { NULL, K_NOT_A_KEY } /* end marker */ +}; int CheckKeyCodes(char* k, MMKeyCode *key) { if (!key) return -1; - if (strcmp(k, "alt") == 0) - { - *key = K_ALT; - } - else if (strcmp(k, "command") == 0) - { - *key = K_META; - } - else if (strcmp(k, "control") == 0) - { - *key = K_CONTROL; - } - else if (strcmp(k, "shift") == 0) - { - *key = K_SHIFT; - } - else if (strcmp(k, "backspace") == 0) - { - *key = K_BACKSPACE; - } - else if (strcmp(k, "enter") == 0) - { - *key = K_RETURN; - } - else if (strcmp(k, "tab") == 0) - { - *key = K_TAB; - } - else if (strcmp(k, "up") == 0) - { - *key = K_UP; - } - else if (strcmp(k, "down") == 0) - { - *key = K_DOWN; - } - else if (strcmp(k, "left") == 0) - { - *key = K_LEFT; - } - else if (strcmp(k, "right") == 0) - { - *key = K_RIGHT; - } - else if (strcmp(k, "escape") == 0) - { - *key = K_ESCAPE; - } - else if (strcmp(k, "delete") == 0) - { - *key = K_DELETE; - } - else if (strcmp(k, "home") == 0) - { - *key = K_HOME; - } - else if (strcmp(k, "end") == 0) - { - *key = K_END; - } - else if (strcmp(k, "pageup") == 0) - { - *key = K_PAGEUP; - } - else if (strcmp(k, "pagedown") == 0) - { - *key = K_PAGEDOWN; - } - else if (strcmp(k, "space") == 0) - { - *key = K_SPACE; - } - else if (strcmp(k, "f1") == 0) - { - *key = K_F1; - } - else if (strcmp(k, "f2") == 0) - { - *key = K_F2; - } - else if (strcmp(k, "f3") == 0) - { - *key = K_F3; - } - else if (strcmp(k, "f4") == 0) - { - *key = K_F4; - } - else if (strcmp(k, "f5") == 0) - { - *key = K_F5; - } - else if (strcmp(k, "f6") == 0) - { - *key = K_F6; - } - else if (strcmp(k, "f7") == 0) - { - *key = K_F7; - } - else if (strcmp(k, "f8") == 0) - { - *key = K_F8; - } - else if (strcmp(k, "f9") == 0) - { - *key = K_F9; - } - else if (strcmp(k, "f10") == 0) - { - *key = K_F10; - } - else if (strcmp(k, "f11") == 0) - { - *key = K_F11; - } - else if (strcmp(k, "f12") == 0) - { - *key = K_F12; - } - else if (strcmp(k, "printscreen") == 0) - { - #if defined(IS_WINDOWS) - *key = K_PRINTSCREEN; - #else - Nan::ThrowError("printscreen is only supported on Windows."); - #endif - } - else if (strlen(k) == 1) + if (strlen(k) == 1) { *key = keyCodeForChar(*k); + return 0; } - else - { + + *key = K_NOT_A_KEY; + + KeyNames* kn = key_names; + while (kn->name) { + if (strcmp(k, kn->name) == 0) + { + *key = kn->key; + break; + } + kn++; + } + + if (*key == K_NOT_A_KEY) { return -2; } From 523d8e18386464dbdb00e468ec49a77471476422 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Mon, 19 Oct 2015 15:49:47 +0300 Subject: [PATCH 2/2] Fix several differences --- src/robotjs.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/robotjs.cc b/src/robotjs.cc index 1df4a562..a9bcf660 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -261,8 +261,8 @@ static KeyNames key_names[] = { "left", K_LEFT }, { "home", K_HOME }, { "end", K_END }, - { "page_up", K_PAGEUP }, - { "page_down", K_PAGEDOWN }, + { "pageup", K_PAGEUP }, + { "pagedown", K_PAGEDOWN }, { "f1", K_F1 }, { "f2", K_F2 }, { "f3", K_F3 }, @@ -279,7 +279,6 @@ static KeyNames key_names[] = { "alt", K_ALT }, { "control", K_CONTROL }, { "shift", K_SHIFT }, - { "caps", K_CAPSLOCK }, { "space", K_SPACE }, { "printscreen", K_PRINTSCREEN },