mirror of
https://github.com/glfw/glfw.git
synced 2026-09-19 15:25:50 +00:00
Added scancode to key callback.
This commit is contained in:
+42
-46
@@ -203,9 +203,9 @@ static void centerCursor(_GLFWwindow *window)
|
||||
|
||||
@end
|
||||
|
||||
// Converts Mac OS X key modifiers into GLFW ones
|
||||
// Translates Mac OS X key modifiers into GLFW ones
|
||||
//
|
||||
static int convertKeyMods(NSUInteger flags)
|
||||
static int translateFlags(NSUInteger flags)
|
||||
{
|
||||
int mods = 0;
|
||||
|
||||
@@ -221,9 +221,9 @@ static int convertKeyMods(NSUInteger flags)
|
||||
return mods;
|
||||
}
|
||||
|
||||
// Converts a Mac OS X keycode to a GLFW keycode
|
||||
// Translates a Mac OS X keycode to a GLFW keycode
|
||||
//
|
||||
static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
static int translateKey(unsigned int key)
|
||||
{
|
||||
// Keyboard symbol translation table
|
||||
// TODO: Need to find mappings for F13-F15, volume down/up/mute, and eject.
|
||||
@@ -281,7 +281,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
/* 31 */ GLFW_KEY_SPACE,
|
||||
/* 32 */ GLFW_KEY_WORLD_1,
|
||||
/* 33 */ GLFW_KEY_BACKSPACE,
|
||||
/* 34 */ -1,
|
||||
/* 34 */ GLFW_KEY_UNKNOWN,
|
||||
/* 35 */ GLFW_KEY_ESCAPE,
|
||||
/* 36 */ GLFW_KEY_RIGHT_SUPER,
|
||||
/* 37 */ GLFW_KEY_LEFT_SUPER,
|
||||
@@ -292,21 +292,21 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
/* 3c */ GLFW_KEY_RIGHT_SHIFT,
|
||||
/* 3d */ GLFW_KEY_RIGHT_ALT,
|
||||
/* 3e */ GLFW_KEY_RIGHT_CONTROL,
|
||||
/* 3f */ -1, /* Function */
|
||||
/* 3f */ GLFW_KEY_UNKNOWN, /* Function */
|
||||
/* 40 */ GLFW_KEY_F17,
|
||||
/* 41 */ GLFW_KEY_KP_DECIMAL,
|
||||
/* 42 */ -1,
|
||||
/* 42 */ GLFW_KEY_UNKNOWN,
|
||||
/* 43 */ GLFW_KEY_KP_MULTIPLY,
|
||||
/* 44 */ -1,
|
||||
/* 44 */ GLFW_KEY_UNKNOWN,
|
||||
/* 45 */ GLFW_KEY_KP_ADD,
|
||||
/* 46 */ -1,
|
||||
/* 46 */ GLFW_KEY_UNKNOWN,
|
||||
/* 47 */ GLFW_KEY_NUM_LOCK, /* Really KeypadClear... */
|
||||
/* 48 */ -1, /* VolumeUp */
|
||||
/* 49 */ -1, /* VolumeDown */
|
||||
/* 4a */ -1, /* Mute */
|
||||
/* 48 */ GLFW_KEY_UNKNOWN, /* VolumeUp */
|
||||
/* 49 */ GLFW_KEY_UNKNOWN, /* VolumeDown */
|
||||
/* 4a */ GLFW_KEY_UNKNOWN, /* Mute */
|
||||
/* 4b */ GLFW_KEY_KP_DIVIDE,
|
||||
/* 4c */ GLFW_KEY_KP_ENTER,
|
||||
/* 4d */ -1,
|
||||
/* 4d */ GLFW_KEY_UNKNOWN,
|
||||
/* 4e */ GLFW_KEY_KP_SUBTRACT,
|
||||
/* 4f */ GLFW_KEY_F18,
|
||||
/* 50 */ GLFW_KEY_F19,
|
||||
@@ -322,26 +322,26 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
/* 5a */ GLFW_KEY_F20,
|
||||
/* 5b */ GLFW_KEY_KP_8,
|
||||
/* 5c */ GLFW_KEY_KP_9,
|
||||
/* 5d */ -1,
|
||||
/* 5e */ -1,
|
||||
/* 5f */ -1,
|
||||
/* 5d */ GLFW_KEY_UNKNOWN,
|
||||
/* 5e */ GLFW_KEY_UNKNOWN,
|
||||
/* 5f */ GLFW_KEY_UNKNOWN,
|
||||
/* 60 */ GLFW_KEY_F5,
|
||||
/* 61 */ GLFW_KEY_F6,
|
||||
/* 62 */ GLFW_KEY_F7,
|
||||
/* 63 */ GLFW_KEY_F3,
|
||||
/* 64 */ GLFW_KEY_F8,
|
||||
/* 65 */ GLFW_KEY_F9,
|
||||
/* 66 */ -1,
|
||||
/* 66 */ GLFW_KEY_UNKNOWN,
|
||||
/* 67 */ GLFW_KEY_F11,
|
||||
/* 68 */ -1,
|
||||
/* 68 */ GLFW_KEY_UNKNOWN,
|
||||
/* 69 */ GLFW_KEY_PRINT_SCREEN,
|
||||
/* 6a */ GLFW_KEY_F16,
|
||||
/* 6b */ GLFW_KEY_F14,
|
||||
/* 6c */ -1,
|
||||
/* 6c */ GLFW_KEY_UNKNOWN,
|
||||
/* 6d */ GLFW_KEY_F10,
|
||||
/* 6e */ -1,
|
||||
/* 6e */ GLFW_KEY_UNKNOWN,
|
||||
/* 6f */ GLFW_KEY_F12,
|
||||
/* 70 */ -1,
|
||||
/* 70 */ GLFW_KEY_UNKNOWN,
|
||||
/* 71 */ GLFW_KEY_F15,
|
||||
/* 72 */ GLFW_KEY_INSERT, /* Really Help... */
|
||||
/* 73 */ GLFW_KEY_HOME,
|
||||
@@ -356,13 +356,13 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
/* 7c */ GLFW_KEY_RIGHT,
|
||||
/* 7d */ GLFW_KEY_DOWN,
|
||||
/* 7e */ GLFW_KEY_UP,
|
||||
/* 7f */ -1,
|
||||
/* 7f */ GLFW_KEY_UNKNOWN,
|
||||
};
|
||||
|
||||
if (macKeyCode >= 128)
|
||||
return -1;
|
||||
if (key >= 128)
|
||||
return GLFW_KEY_UNKNOWN;
|
||||
|
||||
return table[macKeyCode];
|
||||
return table[key];
|
||||
}
|
||||
|
||||
|
||||
@@ -436,7 +436,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
_glfwInputMouseClick(window,
|
||||
GLFW_MOUSE_BUTTON_LEFT,
|
||||
GLFW_PRESS,
|
||||
convertKeyMods([event modifierFlags]));
|
||||
translateFlags([event modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)mouseDragged:(NSEvent *)event
|
||||
@@ -449,7 +449,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
_glfwInputMouseClick(window,
|
||||
GLFW_MOUSE_BUTTON_LEFT,
|
||||
GLFW_RELEASE,
|
||||
convertKeyMods([event modifierFlags]));
|
||||
translateFlags([event modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)mouseMoved:(NSEvent *)event
|
||||
@@ -470,7 +470,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
_glfwInputMouseClick(window,
|
||||
GLFW_MOUSE_BUTTON_RIGHT,
|
||||
GLFW_PRESS,
|
||||
convertKeyMods([event modifierFlags]));
|
||||
translateFlags([event modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)rightMouseDragged:(NSEvent *)event
|
||||
@@ -483,7 +483,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
_glfwInputMouseClick(window,
|
||||
GLFW_MOUSE_BUTTON_RIGHT,
|
||||
GLFW_RELEASE,
|
||||
convertKeyMods([event modifierFlags]));
|
||||
translateFlags([event modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)otherMouseDown:(NSEvent *)event
|
||||
@@ -491,7 +491,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
_glfwInputMouseClick(window,
|
||||
[event buttonNumber],
|
||||
GLFW_PRESS,
|
||||
convertKeyMods([event modifierFlags]));
|
||||
translateFlags([event modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)otherMouseDragged:(NSEvent *)event
|
||||
@@ -504,7 +504,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
_glfwInputMouseClick(window,
|
||||
[event buttonNumber],
|
||||
GLFW_RELEASE,
|
||||
convertKeyMods([event modifierFlags]));
|
||||
translateFlags([event modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)mouseExited:(NSEvent *)event
|
||||
@@ -548,14 +548,11 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
|
||||
- (void)keyDown:(NSEvent *)event
|
||||
{
|
||||
const NSUInteger mods = [event modifierFlags];
|
||||
const int key = translateKey([event keyCode]);
|
||||
const int mods = translateFlags([event modifierFlags]);
|
||||
_glfwInputKey(window, key, [event keyCode], GLFW_PRESS, mods);
|
||||
|
||||
_glfwInputKey(window,
|
||||
convertMacKeyCode([event keyCode]),
|
||||
GLFW_PRESS,
|
||||
convertKeyMods(mods));
|
||||
|
||||
if ([event modifierFlags] & NSCommandKeyMask)
|
||||
if (mods & GLFW_MOD_SUPER)
|
||||
return;
|
||||
|
||||
NSString* characters = [event characters];
|
||||
@@ -567,7 +564,7 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
|
||||
- (void)flagsChanged:(NSEvent *)event
|
||||
{
|
||||
int action, key;
|
||||
int action;
|
||||
unsigned int newModifierFlags =
|
||||
[event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
|
||||
|
||||
@@ -578,17 +575,16 @@ static int convertMacKeyCode(unsigned int macKeyCode)
|
||||
|
||||
window->ns.modifierFlags = newModifierFlags;
|
||||
|
||||
key = convertMacKeyCode([event keyCode]);
|
||||
if (key != -1)
|
||||
_glfwInputKey(window, key, action, convertKeyMods([event modifierFlags]));
|
||||
const int key = translateKey([event keyCode]);
|
||||
const int mods = translateFlags([event modifierFlags]);
|
||||
_glfwInputKey(window, key, [event keyCode], action, mods);
|
||||
}
|
||||
|
||||
- (void)keyUp:(NSEvent *)event
|
||||
{
|
||||
_glfwInputKey(window,
|
||||
convertMacKeyCode([event keyCode]),
|
||||
GLFW_RELEASE,
|
||||
convertKeyMods([event modifierFlags]));
|
||||
const int key = translateKey([event keyCode]);
|
||||
const int mods = translateFlags([event modifierFlags]);
|
||||
_glfwInputKey(window, key, [event keyCode], GLFW_RELEASE, mods);
|
||||
}
|
||||
|
||||
- (void)scrollWheel:(NSEvent *)event
|
||||
|
||||
+11
-11
@@ -122,29 +122,29 @@ static void setStickyMouseButtons(_GLFWwindow* window, int enabled)
|
||||
////// GLFW event API //////
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void _glfwInputKey(_GLFWwindow* window, int key, int action, int mods)
|
||||
void _glfwInputKey(_GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
GLboolean repeated = GL_FALSE;
|
||||
|
||||
if (key < 0 || key > GLFW_KEY_LAST)
|
||||
return;
|
||||
|
||||
if (action == GLFW_RELEASE && window->key[key] == GLFW_RELEASE)
|
||||
return;
|
||||
|
||||
if (action == GLFW_PRESS && window->key[key] == GLFW_PRESS)
|
||||
repeated = GL_TRUE;
|
||||
if (key >= 0 && key <= GLFW_KEY_LAST)
|
||||
{
|
||||
if (action == GLFW_PRESS && window->key[key] == GLFW_PRESS)
|
||||
repeated = GL_TRUE;
|
||||
|
||||
if (action == GLFW_RELEASE && window->stickyKeys)
|
||||
window->key[key] = _GLFW_STICK;
|
||||
else
|
||||
window->key[key] = (char) action;
|
||||
if (action == GLFW_RELEASE && window->stickyKeys)
|
||||
window->key[key] = _GLFW_STICK;
|
||||
else
|
||||
window->key[key] = (char) action;
|
||||
}
|
||||
|
||||
if (repeated)
|
||||
action = GLFW_REPEAT;
|
||||
|
||||
if (window->callbacks.key)
|
||||
window->callbacks.key((GLFWwindow*) window, key, action, mods);
|
||||
window->callbacks.key((GLFWwindow*) window, key, scancode, action, mods);
|
||||
}
|
||||
|
||||
void _glfwInputChar(_GLFWwindow* window, unsigned int character)
|
||||
|
||||
+2
-1
@@ -606,11 +606,12 @@ void _glfwInputWindowCloseRequest(_GLFWwindow* window);
|
||||
/*! @brief Notifies shared code of a physical key event.
|
||||
* @param[in] window The window that received the event.
|
||||
* @param[in] key The key that was pressed or released.
|
||||
* @param[in] scancode The system-specific scan code of the key.
|
||||
* @param[in] action @ref GLFW_PRESS or @ref GLFW_RELEASE.
|
||||
* @param[in] mods The modifiers pressed when the event was generated.
|
||||
* @ingroup event
|
||||
*/
|
||||
void _glfwInputKey(_GLFWwindow* window, int key, int action, int mods);
|
||||
void _glfwInputKey(_GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
|
||||
/*! @brief Notifies shared code of a Unicode character input event.
|
||||
* @param[in] window The window that received the event.
|
||||
|
||||
+22
-11
@@ -34,6 +34,8 @@
|
||||
#include <malloc.h>
|
||||
#include <windowsx.h>
|
||||
|
||||
#define _GLFW_KEY_INVALID -2
|
||||
|
||||
|
||||
// Updates the cursor clip rect
|
||||
//
|
||||
@@ -214,7 +216,7 @@ static int translateKey(WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
// Next message is a RALT down message, which
|
||||
// means that this is not a proper LCTRL message
|
||||
return -1;
|
||||
return _GLFW_KEY_INVALID;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -360,8 +362,8 @@ static int translateKey(WPARAM wParam, LPARAM lParam)
|
||||
default: break;
|
||||
}
|
||||
|
||||
// No matching translation was found, so return -1
|
||||
return -1;
|
||||
// No matching translation was found
|
||||
return GLFW_KEY_UNKNOWN;
|
||||
}
|
||||
|
||||
// Window callback function (handles window events)
|
||||
@@ -471,7 +473,12 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
||||
case WM_KEYDOWN:
|
||||
case WM_SYSKEYDOWN:
|
||||
{
|
||||
_glfwInputKey(window, translateKey(wParam, lParam), GLFW_PRESS, getKeyMods());
|
||||
const int scancode = (lParam >> 16) & 0xff;
|
||||
const int key = translateKey(wParam, lParam);
|
||||
if (key == _GLFW_KEY_INVALID)
|
||||
break;
|
||||
|
||||
_glfwInputKey(window, key, scancode, GLFW_PRESS, getKeyMods());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -500,22 +507,26 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
||||
case WM_SYSKEYUP:
|
||||
{
|
||||
const int mods = getKeyMods();
|
||||
const int scancode = (lParam >> 16) & 0xff;
|
||||
const int key = translateKey(wParam, lParam);
|
||||
if (key == _GLFW_KEY_INVALID)
|
||||
break;
|
||||
|
||||
if (wParam == VK_SHIFT)
|
||||
{
|
||||
// Release both Shift keys on Shift up event, as only one event
|
||||
// is sent even if both keys are released
|
||||
_glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, GLFW_RELEASE, mods);
|
||||
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, GLFW_RELEASE, mods);
|
||||
_glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, scancode, GLFW_RELEASE, mods);
|
||||
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, scancode, GLFW_RELEASE, mods);
|
||||
}
|
||||
else if (wParam == VK_SNAPSHOT)
|
||||
{
|
||||
// Key down is not reported for the print screen key
|
||||
_glfwInputKey(window, GLFW_KEY_PRINT_SCREEN, GLFW_PRESS, mods);
|
||||
_glfwInputKey(window, GLFW_KEY_PRINT_SCREEN, GLFW_RELEASE, mods);
|
||||
_glfwInputKey(window, key, scancode, GLFW_PRESS, mods);
|
||||
_glfwInputKey(window, key, scancode, GLFW_RELEASE, mods);
|
||||
}
|
||||
else
|
||||
_glfwInputKey(window, translateKey(wParam, lParam), GLFW_RELEASE, getKeyMods());
|
||||
_glfwInputKey(window, key, scancode, GLFW_RELEASE, mods);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -1064,10 +1075,10 @@ void _glfwPlatformPollEvents(void)
|
||||
// See if this differs from our belief of what has happened
|
||||
// (we only have to check for lost key up events)
|
||||
if (!lshiftDown && window->key[GLFW_KEY_LEFT_SHIFT] == 1)
|
||||
_glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, GLFW_RELEASE, mods);
|
||||
_glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, 0, GLFW_RELEASE, mods);
|
||||
|
||||
if (!rshiftDown && window->key[GLFW_KEY_RIGHT_SHIFT] == 1)
|
||||
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, GLFW_RELEASE, mods);
|
||||
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, 0, GLFW_RELEASE, mods);
|
||||
}
|
||||
|
||||
// Did the cursor move in an focused window that has captured the cursor
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ void _glfwInputWindowFocus(_GLFWwindow* window, GLboolean focused)
|
||||
for (i = 0; i <= GLFW_KEY_LAST; i++)
|
||||
{
|
||||
if (window->key[i] == GLFW_PRESS)
|
||||
_glfwInputKey(window, i, GLFW_RELEASE, 0);
|
||||
_glfwInputKey(window, i, 0, GLFW_RELEASE, 0);
|
||||
}
|
||||
|
||||
// Release all pressed mouse buttons
|
||||
|
||||
+7
-7
@@ -39,13 +39,13 @@
|
||||
|
||||
// Translate an X11 key code to a GLFW key code.
|
||||
//
|
||||
static int keyCodeToGLFWKeyCode(int keyCode)
|
||||
static int translateKey(int keyCode)
|
||||
{
|
||||
int keySym;
|
||||
|
||||
// Valid key code range is [8,255], according to the XLib manual
|
||||
if (keyCode < 8 || keyCode > 255)
|
||||
return -1;
|
||||
return GLFW_KEY_UNKNOWN;
|
||||
|
||||
// Try secondary keysym, for numeric keypad keys
|
||||
// Note: This way we always force "NumLock = ON", which is intentional
|
||||
@@ -211,8 +211,8 @@ static int keyCodeToGLFWKeyCode(int keyCode)
|
||||
default: break;
|
||||
}
|
||||
|
||||
// No matching translation was found, so return -1
|
||||
return -1;
|
||||
// No matching translation was found
|
||||
return GLFW_KEY_UNKNOWN;
|
||||
}
|
||||
|
||||
// Update the key code LUT
|
||||
@@ -225,7 +225,7 @@ static void updateKeyCodeLUT(void)
|
||||
|
||||
// Clear the LUT
|
||||
for (keyCode = 0; keyCode < 256; keyCode++)
|
||||
_glfw.x11.keyCodeLUT[keyCode] = -1;
|
||||
_glfw.x11.keyCodeLUT[keyCode] = GLFW_KEY_UNKNOWN;
|
||||
|
||||
// Use XKB to determine physical key locations independently of the current
|
||||
// keyboard layout
|
||||
@@ -296,7 +296,7 @@ static void updateKeyCodeLUT(void)
|
||||
else if (strcmp(name, "AB10") == 0) keyCodeGLFW = GLFW_KEY_SLASH;
|
||||
else if (strcmp(name, "BKSL") == 0) keyCodeGLFW = GLFW_KEY_BACKSLASH;
|
||||
else if (strcmp(name, "LSGT") == 0) keyCodeGLFW = GLFW_KEY_WORLD_1;
|
||||
else keyCodeGLFW = -1;
|
||||
else keyCodeGLFW = GLFW_KEY_UNKNOWN;
|
||||
|
||||
// Update the key code LUT
|
||||
if ((keyCode >= 0) && (keyCode < 256))
|
||||
@@ -311,7 +311,7 @@ static void updateKeyCodeLUT(void)
|
||||
for (keyCode = 0; keyCode < 256; keyCode++)
|
||||
{
|
||||
if (_glfw.x11.keyCodeLUT[keyCode] < 0)
|
||||
_glfw.x11.keyCodeLUT[keyCode] = keyCodeToGLFWKeyCode(keyCode);
|
||||
_glfw.x11.keyCodeLUT[keyCode] = translateKey(keyCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -83,8 +83,8 @@ static int translateKey(int keycode)
|
||||
// Use the pre-filled LUT (see updateKeyCodeLUT() in x11_init.c)
|
||||
if ((keycode >= 0) && (keycode < 256))
|
||||
return _glfw.x11.keyCodeLUT[keycode];
|
||||
else
|
||||
return -1;
|
||||
|
||||
return GLFW_KEY_UNKNOWN;
|
||||
}
|
||||
|
||||
// Translates an X Window event to Unicode
|
||||
@@ -515,7 +515,7 @@ static void processEvent(XEvent *event)
|
||||
const int key = translateKey(event->xkey.keycode);
|
||||
const int mods = translateState(event->xkey.state);
|
||||
|
||||
_glfwInputKey(window, key, GLFW_PRESS, mods);
|
||||
_glfwInputKey(window, key, event->xkey.keycode, GLFW_PRESS, mods);
|
||||
|
||||
if (!(mods & GLFW_MOD_CONTROL) && !(mods & GLFW_MOD_ALT))
|
||||
_glfwInputChar(window, translateChar(&event->xkey));
|
||||
@@ -528,7 +528,7 @@ static void processEvent(XEvent *event)
|
||||
const int key = translateKey(event->xkey.keycode);
|
||||
const int mods = translateState(event->xkey.state);
|
||||
|
||||
_glfwInputKey(window, key, GLFW_RELEASE, mods);
|
||||
_glfwInputKey(window, key, event->xkey.keycode, GLFW_RELEASE, mods);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user