Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#2043 closed enhancement (fixed)

[PATCH] Support for extra keys usefull for hotkeys on non qwerty keybords on Linux and OS X.

Reported by: lsdh Owned by: ben
Priority: Should Have Milestone: Alpha 14
Component: Core engine Keywords: simple patch
Cc: Patch:

Description (last modified by lsdh)

On Linux (tested on Ubuntu 13.4 with gnome shell); the hotkeys ctrl+1 to ctrl+0 did not work natively in my case (but does in Windows 7, same computer, same keyboard layout). I have a Belgian keyboard, so default keymapping is really different from the US one.

A ~/.config/0ad/config/local.cfg can receive modifications to have custom hotkeys, but some keys I needed where not available: § à ç è é

Theses keys are (in sdl 1.2) part of the SDLK_WORLD keys.

Unfortunately, the numbers used are not always the same (at least different in OSX with french keyboard).

To allow people to have a full ctrl+1 to ctrl+0 bar without changing the sources and to anticipate the possible keypapping in the GUI, I added the full SDLK_WORLD key list in the /0ad/source/ps/Keyname.cpp list.

As the SDLK_WORLD are no more there in sdl2, I putted the definition there:

#if SDL_VERSION_ATLEAST(2, 0, 0)
	{ SDLK_KP_0, "Numpad 0", "Num0" },
	{ SDLK_KP_1, "Numpad 1", "Num1" },
	{ SDLK_KP_2, "Numpad 2", "Num2" },
	{ SDLK_KP_3, "Numpad 3", "Num3" },
	{ SDLK_KP_4, "Numpad 4", "Num4" },
	{ SDLK_KP_5, "Numpad 5", "Num5" },
	{ SDLK_KP_6, "Numpad 6", "Num6" },
	{ SDLK_KP_7, "Numpad 7", "Num7" },
	{ SDLK_KP_8, "Numpad 8", "Num8" },
	{ SDLK_KP_9, "Numpad 9", "Num9" },
#else
	{ SDLK_WORLD_0, "World0", "W0" },
	.
	.
	.
	{ SDLK_WORLD_95, "World95", "W95" },

	{ SDLK_KP0, "Numpad 0", "Num0" },
	{ SDLK_KP1, "Numpad 1", "Num1" },
	{ SDLK_KP2, "Numpad 2", "Num2" },
	{ SDLK_KP3, "Numpad 3", "Num3" },
	{ SDLK_KP4, "Numpad 4", "Num4" },
	{ SDLK_KP5, "Numpad 5", "Num5" },
	{ SDLK_KP6, "Numpad 6", "Num6" },
	{ SDLK_KP7, "Numpad 7", "Num7" },
	{ SDLK_KP8, "Numpad 8", "Num8" },
	{ SDLK_KP9, "Numpad 9", "Num9" },
#endif

And configured my ~/.config/0ad/config/local.cfg like this: (This is only there to help people that would like to configure theses keys themselves).

hotkey.selection.group.select.0 = W64 
hotkey.selection.group.save.0 = "Ctrl+W64" 
hotkey.selection.group.add.0 = "Shift+W64"
hotkey.selection.group.select.1 = Ampersand
hotkey.selection.group.save.1 = "Ctrl+Ampersand"
hotkey.selection.group.add.1 = "Shift+Ampersand"
hotkey.selection.group.select.2 = W73 
hotkey.selection.group.save.2 = "Ctrl+W73" 
hotkey.selection.group.add.2 = "Shift+W73" 
hotkey.selection.group.select.3 = DoubleQuote
hotkey.selection.group.save.3 = "Ctrl+DoubleQuote"
hotkey.selection.group.add.3 = "Shift+DoubleQuote"
hotkey.selection.group.select.4 = SingleQuote
hotkey.selection.group.save.4 = "Ctrl+SingleQuote"
hotkey.selection.group.add.4 = "Shift+SingleQuote"
hotkey.selection.group.select.5 = LeftParen
hotkey.selection.group.save.5 = "Ctrl+LeftParen"
hotkey.selection.group.add.5 = "Shift+LeftParen"
hotkey.selection.group.select.6 = W7 
hotkey.selection.group.save.6 = "Ctrl+W7" 
hotkey.selection.group.add.6 = "Shift+W7" 
hotkey.selection.group.select.7 = W72 
hotkey.selection.group.save.7 = "Ctrl+W72" 
hotkey.selection.group.add.7 = "Shift+W72" 
hotkey.selection.group.select.8 = Exclaim
hotkey.selection.group.save.8 = "Ctrl+Exclaim"
hotkey.selection.group.add.8 = "Shift+Exclaim"
hotkey.selection.group.select.9 = W71 
hotkey.selection.group.save.9 = "Ctrl+W71" 
hotkey.selection.group.add.9 = "Shift+W71" 

The /0ad/binaries/data/config/keys.txt should also be updated to warn theses keys are available.

I could manage to do this changes thanks to Philip` and RedFox| on the IRC, so a big thanks to them.



(To know which SDLK_WORLD number correspond to a keys, you can temporary add the line

printf("%d %d\n", ev->ev.type, (int)ev->ev.key.keysym.sym);

in the HotkeyInputHandler function of /0ad/source/ps/Hotkey.cpp then compile and launch the game in a terminal. An integer will be displayed when a key is pressed:

2 224
3 224

The second number (224 here) correspond to the key and this http://www-personal.umich.edu/~bazald/l/api/_s_d_l__keysym_8h.html will give you the SDLK name corresponding).

Attachments (1)

KeyName.cpp.patch (4.0 KB ) - added by lsdh 11 years ago.
Adding the SDLK_WORLD_0 to SDLK_WORLD_95 keys.

Download all attachments as: .zip

Change History (26)

comment:1 by lsdh, 11 years ago

Description: modified (diff)

comment:2 by lsdh, 11 years ago

Description: modified (diff)

comment:3 by sanderd17, 11 years ago

Some comments:

  • It's better to have all changes in one diff, that can be applied a lot easier. You should use "svn diff" on the root svn directory
  • Don't add debug code (like that printf line) to the changes. You're welcome to mention it in the description, but it shouldn't be added just like that
  • Is it possible to use better names than W7, W72 etc? It's not because SDL uses those names that they should be in the config file.
  • Those changes shouldn't be in the config file (we won't include it, as Belgian keyboards are a clear minority compared to QWERTY), but they should be added to the txt documentation file if people want to use it, or as reference for when 0AD gets multiple keyboard layout support.

But thanks for the time, it's certainly worth supporting extra keys.

comment:4 by sanderd17, 11 years ago

Keywords: patch added; Keyname removed
Priority: If Time PermitsShould Have
Summary: Adding some Belgian keymapping for the hotkeys[PATCH] Adding some Belgian keymapping for the hotkeys

comment:5 by lsdh, 11 years ago

I'll update tomorrow.

In fact, the only two files that should be in the snv diff are the KeyName.cpp​ and the keys.txt. the .cfg file is there if people wants to easily have it to works and the Hotkey.cpp can be deleted from the list (it only contain the commented line, so it does not helps).

With theses changes, the keys would be right for Belgian and French azerty keyboards (with little double usage of - as this key is already used for zoom-).

I see that most qwerty keyboards have the numbers at this place, so not causing problem I guess, but some special keyboards still exist, like the Lithuanian one.

Does it worth I add their specials keys?

Thanks for your interest :-)

Last edited 11 years ago by lsdh (previous) (diff)

comment:6 by lsdh, 11 years ago

Description: modified (diff)
Summary: [PATCH] Adding some Belgian keymapping for the hotkeys[PATCH] Adding some azerty keymapping for the hotkeys

comment:7 by sanderd17, 11 years ago

Keywords: review added

in reply to:  description comment:8 by historic_bruno, 11 years ago

Milestone: BacklogAlpha 14

Replying to lsdh:

On Linux (tested on Ubuntu 13.4 with gnome shell); the hotkeys ctrl+1 to ctrl+0 did not work natively in my case (but does in Windows 7, same computer). I have a Belgian keyboard, so default keymapping is really different from the US one.

That's unfortunate, it seems SDL2 will have a better system with virtual keys that map to physical keys, respecting layouts, but until then I guess you'll need different hotkeys :(

Do we know that e.g. SDLK_WORLD_71 will always map to 'ç' or does that only apply to some European/Belgian keyboard layouts? The SDL 1.2 documentation doesn't say so.

Last edited 11 years ago by historic_bruno (previous) (diff)

comment:9 by wraitii, 11 years ago

Tested on a French Apple keyboard... And to put shortly it won't work.

"Eacute" would be SDLK_WORLD_0, "Eagrave" would be SDLK_WORLD_3, "Agrave" would be SDLK_WORLD_4 and "Ccedil" would be SDLK_WORLD_2. SDLK_WORLD_1 is probably something like ù.

comment:10 by lsdh, 11 years ago

Description: modified (diff)

comment:11 by lsdh, 11 years ago

If there is no general rule for the SDLK_WORLD_x to correspond to a key, an idea could be to add the whole list of SDLK_WORLD_0 to SDLK_WORLD_95, with generics names (ea W0 to W95). Having all the list would also increase the range of compatibility (the Lithuanian keys could be in theses places).

Than each user would just have to manage to find the needed ones for him.

The main problem is that it is not trivial to know witch keys represent the willing ones (need to capture the keysym, than make the conversion to the Wnumber).

But, there is still a feature that is not yet usable, the "Tool & options> Options" in the gui. This type of option could do this job: The player press the key related to the shortcut he wants, and the computer handle the more tricky parts.

So, until the "Tool & options> Options" is not yet implemented, the whole list could however be added so people that would be interested to config themselves would have exactly the same sources and bin, just the config file would be different.

I just hope adding theses 96 more keys would not slow the game (I think it would be negligible, but I'm not en expert).

What's your point of view?

comment:12 by historic_bruno, 11 years ago

Right, eventually we should be able to set hotkeys in-game. As a workaround we could add some code to optionally display the SDLKey name of pressed keys, to make it easier to add hotkeys for non-US keyboards.

I wouldn't expect there to be any performance impact from having more possible keys, and they won't even be used by default. One thing to consider is whether/how this works with our Windows SDL emulation. You say the hotkeys already work in Windows, are you using a Belgian layout there?

Also I'm planning to switch to SDL2 on Windows, it's more reliable than our custom emulation and has better multimonitor support. But it will be annoying if we add many new hotkeys that don't have a match in SDL2 (SDL_Keycode doesn't have generic "world" keys).

comment:13 by lsdh, 11 years ago

Yes, I'm using Belgian Layout under Windows 7.

It's possible that this W7 has sdl2, that could explain the thing as there is already some "#if SDL_VERSION_ATLEAST(2, 0, 0)" compiler tags (Didn't explicitly check if it works both on alpha13 and SVN, but I think so).

In this list for SDL version before 2: http://sdl.beuc.net/sdl.wiki/SDLKey (all our list is in this order, just the world keys and a few others are missing), the place of the worlds key are before the numpad keys:

#if SDL_VERSION_ATLEAST(2, 0, 0)
	{ SDLK_KP_0, "Numpad 0", "Num0" },
	{ SDLK_KP_1, "Numpad 1", "Num1" },
	{ SDLK_KP_2, "Numpad 2", "Num2" },
	{ SDLK_KP_3, "Numpad 3", "Num3" },
	{ SDLK_KP_4, "Numpad 4", "Num4" },
	{ SDLK_KP_5, "Numpad 5", "Num5" },
	{ SDLK_KP_6, "Numpad 6", "Num6" },
	{ SDLK_KP_7, "Numpad 7", "Num7" },
	{ SDLK_KP_8, "Numpad 8", "Num8" },
	{ SDLK_KP_9, "Numpad 9", "Num9" },
#else
	{ SDLK_WORLD_0, "World0", "W0" },
	.
	.
	.
	{ SDLK_WORLD_95, "World95", "W95" },

	{ SDLK_KP0, "Numpad 0", "Num0" },
	{ SDLK_KP1, "Numpad 1", "Num1" },
	{ SDLK_KP2, "Numpad 2", "Num2" },
	{ SDLK_KP3, "Numpad 3", "Num3" },
	{ SDLK_KP4, "Numpad 4", "Num4" },
	{ SDLK_KP5, "Numpad 5", "Num5" },
	{ SDLK_KP6, "Numpad 6", "Num6" },
	{ SDLK_KP7, "Numpad 7", "Num7" },
	{ SDLK_KP8, "Numpad 8", "Num8" },
	{ SDLK_KP9, "Numpad 9", "Num9" },
#endif

So I will do like this, and test to compile under W7 to see if all still works correctly.

in reply to:  13 comment:14 by historic_bruno, 11 years ago

Replying to lsdh:

Yes, I'm using Belgian Layout under Windows 7.

It's possible that this W7 has sdl2, that could explain the thing as there is already some "#if SDL_VERSION_ATLEAST(2, 0, 0)" compiler tags (Didn't explicitly check if it works both on alpha13 and SVN, but I think so).

You would know if you were using SDL2 because you'd have to disable our "wsdl" that is hardocded in the engine, and change the build system slightly, and have SDL2 dev libraries compiled and placed in our libraries directory so the game finds them :) And if you're using the latest SDL2 release candidate, you would have to fix the build breakages described on #2041. (The Windows build doesn't even use the v1.2 SDL.dll in binaries/system)

Version 0, edited 11 years ago by historic_bruno (next)

comment:15 by lsdh, 11 years ago

So I definitely do not use the sdl2.

I made the new /0ad/source/ps/Keyname.cpp. It works well on my Ubuntu 13.04 64bit (once the local.cfg set accordingly to the new names). It works well on my Windows 7 64bit (if no local.cfg, still working like before, if the ubuntu local.cfg, the shortcut do not work as expected).

Thanks for the cleaning of the files. This is more clear now.

For the /0ad/binaries/data/config/keys.txt ; it may be a good idea to add the information that theses World1 to World95 are also available.

Last edited 11 years ago by lsdh (previous) (diff)

by lsdh, 11 years ago

Attachment: KeyName.cpp.patch added

Adding the SDLK_WORLD_0 to SDLK_WORLD_95 keys.

comment:16 by lsdh, 11 years ago

Description: modified (diff)

comment:17 by lsdh, 11 years ago

Description: modified (diff)

comment:18 by lsdh, 11 years ago

Description: modified (diff)
Summary: [PATCH] Adding some azerty keymapping for the hotkeys[PATCH] Adding some non US keymapping for the hotkeys

comment:19 by lsdh, 11 years ago

Keywords: simple added
Summary: [PATCH] Adding some non US keymapping for the hotkeys[PATCH] Support for extra keys usefull for hotkeys on non US keybords on Linux and OS X.

comment:20 by historic_bruno, 11 years ago

I don't like that the patch doesn't work on Windows, that will only be confusing to people with non-US keyboards on Windows, and as we support both SDL1 and SDL2 it adds extra confusion - we'll have to ignore SDLK_WORLD_* since there's no equivalent in SDL2 (what happens if an invalid hotkey is encountered, is that an error or ignored?)

I would like to know if our Windows SDL emulation is buggy and differs from standard SDL, or if the handling of layouts is a real difference in SDL1 between Windows and Linux. It would be surprising, SDL is supposed to abstract platform differences as much as possible.

comment:21 by sanderd17, 11 years ago

@historic_bruno. the 1 key is seen as 1 on most keyboard layouts.

But on an AZERTY layout, to get the 1, you need to press shift. In Windows, SDL still recognises that key as 1, but in Linux, it recognises that key as the symbol under the 1. And in some cases, that symbol isn't part of the ASCII range, so it's an SDL_WORLD key.

So it already works for windows with the default settings. This is only a way to give the ability to Linux users with AZERTY keyboard to use the same keys.

comment:22 by lsdh, 11 years ago

Summary: [PATCH] Support for extra keys usefull for hotkeys on non US keybords on Linux and OS X.[PATCH] Support for extra keys usefull for hotkeys on non qwerty keybords on Linux and OS X.

comment:23 by ben, 11 years ago

Owner: set to ben
Resolution: fixed
Status: newclosed

In 13627:

Improves hotkey support for non-QWERTY keyboards, patch by lsdh, fixes #2043

comment:24 by ben, 11 years ago

In 13628:

Updates keys.txt with new keynames, refs #2043

comment:25 by leper, 11 years ago

Keywords: review removed
Note: See TracTickets for help on using tickets.