Fixed modifiers causing sf::Keyboard::Unknown being returned in key events on Unix (#1012). On Unix, SFML now tries harder to create proper key events on keyboards that shift keys which are typically unshifted on QWERTY layouts (this makes the numeric codes usable even on AZERTY layouts).

This commit is contained in:
binary1248 2015-12-24 18:31:15 +01:00
parent 7ff9478061
commit f037c2775b

View File

@ -1757,17 +1757,23 @@ bool WindowImplX11::processEvent(XEvent windowEvent)
// Key down event // Key down event
case KeyPress: case KeyPress:
{ {
// Get the keysym of the key that has been pressed Keyboard::Key key = Keyboard::Unknown;
static XComposeStatus keyboard;
char buffer[32]; // Try each KeySym index (modifier group) until we get a match
KeySym symbol; for (int i = 0; i < 4; ++i)
XLookupString(&windowEvent.xkey, buffer, sizeof(buffer), &symbol, &keyboard); {
// Get the SFML keyboard code from the keysym of the key that has been pressed
key = keysymToSF(XLookupKeysym(&windowEvent.xkey, i));
if (key != Keyboard::Unknown)
break;
}
// Fill the event parameters // Fill the event parameters
// TODO: if modifiers are wrong, use XGetModifierMapping to retrieve the actual modifiers mapping // TODO: if modifiers are wrong, use XGetModifierMapping to retrieve the actual modifiers mapping
Event event; Event event;
event.type = Event::KeyPressed; event.type = Event::KeyPressed;
event.key.code = keysymToSF(symbol); event.key.code = key;
event.key.alt = windowEvent.xkey.state & Mod1Mask; event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.control = windowEvent.xkey.state & ControlMask; event.key.control = windowEvent.xkey.state & ControlMask;
event.key.shift = windowEvent.xkey.state & ShiftMask; event.key.shift = windowEvent.xkey.state & ShiftMask;
@ -1826,15 +1832,22 @@ bool WindowImplX11::processEvent(XEvent windowEvent)
// Key up event // Key up event
case KeyRelease: case KeyRelease:
{ {
// Get the keysym of the key that has been pressed Keyboard::Key key = Keyboard::Unknown;
char buffer[32];
KeySym symbol; // Try each KeySym index (modifier group) until we get a match
XLookupString(&windowEvent.xkey, buffer, 32, &symbol, NULL); for (int i = 0; i < 4; ++i)
{
// Get the SFML keyboard code from the keysym of the key that has been released
key = keysymToSF(XLookupKeysym(&windowEvent.xkey, i));
if (key != Keyboard::Unknown)
break;
}
// Fill the event parameters // Fill the event parameters
Event event; Event event;
event.type = Event::KeyReleased; event.type = Event::KeyReleased;
event.key.code = keysymToSF(symbol); event.key.code = key;
event.key.alt = windowEvent.xkey.state & Mod1Mask; event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.control = windowEvent.xkey.state & ControlMask; event.key.control = windowEvent.xkey.state & ControlMask;
event.key.shift = windowEvent.xkey.state & ShiftMask; event.key.shift = windowEvent.xkey.state & ShiftMask;