Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added special multimedia keycodes + restructured code #106

Merged
merged 2 commits into from
Dec 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions src/keycode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C"
#include <ApplicationServices/ApplicationServices.h>

enum _MMKeyCode {
K_NOT_A_KEY = -1,
K_BACKSPACE = kVK_Delete,
K_DELETE = kVK_ForwardDelete,
K_RETURN = kVK_Return,
Expand Down Expand Up @@ -45,16 +46,38 @@ 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;

#elif defined(USE_X11)

#include <X11/Xutil.h>
#include <X11/XF86keysym.h>

enum _MMKeyCode {
K_NOT_A_KEY = -1,
K_BACKSPACE = XK_BackSpace,
K_DELETE = XK_Delete,
K_RETURN = XK_Return,
Expand Down Expand Up @@ -85,14 +108,35 @@ 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;

#elif defined(IS_WINDOWS)

enum _MMKeyCode {
K_NOT_A_KEY = -1,
K_BACKSPACE = VK_BACK,
K_DELETE = VK_DELETE,
K_RETURN = VK_RETURN,
Expand Down Expand Up @@ -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;
Expand Down
207 changes: 76 additions & 131 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,145 +243,90 @@ 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 },
{ "pageup", K_PAGEUP },
{ "pagedown", 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 },
{ "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;
}

Expand Down