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

glfw does not distinguish between numpad keys based on num lock state #946

Closed
kovidgoyal opened this issue Feb 12, 2017 · 15 comments
Closed
Assignees
Labels
enhancement Feature suggestions and PRs macOS Mir Wayland Windows Win32 specific (not Cygwin or WSL) X11
Milestone

Comments

@kovidgoyal
Copy link
Contributor

glfw returns the exact same key and scan codes regardless of whether num lock is on or off. This makes it impossible to distinguish between KP_7 and KP_HOME for example.

Also glfwGetKeyName() returns NULL for numpad keys.

The list of glfw keys should be extended to add KP_HOME, KP_END, KP_INS, KP_DEL, KP_PG_DN and KP_PG_UP and glfw should return the appropriate key depending on what the underlying window system reports.

On X11, for example, xev is able to distinguish between KP_7 and KP_HOME, reporting them correctly.

@elmindreda elmindreda self-assigned this Feb 12, 2017
@elmindreda elmindreda added the enhancement Feature suggestions and PRs label Feb 13, 2017
@elmindreda
Copy link
Member

This is definitely a hole in the API. However, key tokens like GLFW_KP_7 represent physical keys and not the function assigned to them by the OS, and changing that during 3.x would break existing applications.

Instead, I can add queries for lock key states and you can choose the behavior of the key based on the Num Lock state. Would that be sufficient for your purposes?

@kovidgoyal
Copy link
Contributor Author

Being able to query num lock state is certainly a first step, however, it is a hack -- since the behavior of num lock is configurable at the OS level, and applications should follow the OS as much as possible. Otherwise, glfw based applications will behave differently than all other applications on systems where the behavior of num lock has been changed.

The ideal solution would be a key callback API that reports logical key names as reported by the OS after any keyboard layout/lock key mapping etc takes place. This will not break existing applications, which could continue to use the old API. The new API could either use a new callback function or a switch that changes the behavior of the existing one at runtime.

@elmindreda
Copy link
Member

elmindreda commented Feb 14, 2017

Yup, it won't work on systems with home-made keyboard layouts. Normally I'd argue that this is extremely rare, but you're writing a terminal emulator so that probably isn't true for your users.

The input system for GLFW is by design a simplified abstraction of the OS APIs geared towards games and related tools. I won't add another key callback but once it's time for 4.0 we can look into improving the existing key event. For now, with many higher priority features still to be implemented, lock key state is what I can offer.

@kovidgoyal
Copy link
Contributor Author

Sure, I wont refuse the ability to probe the lock key state :)

And let me just say thanks for glfw, which apart from a few minor issues, works very well.

@ben1
Copy link
Contributor

ben1 commented May 31, 2017

Either solution would be useful. I'm currently hacking around this by using a Windows system call.

@elmindreda
Copy link
Member

elmindreda commented Jul 9, 2017

I'm currently considering one of the following solutions:

Option 1: A function to query the current lock state:

int glfwGetKeyLock(GLFWwindow* window, int key);

Where key is one of GLFW_KEY_*_LOCK. The returned value would be up-to-date wrt the event currently being processed (think GetKeyState, not GetAsyncKeyState).

Option 2: Additional bits in the modifiers passed to the key and charmods callbacks:

#define GLFW_MOD_CAPS_LOCK 0x0010
#define GLFW_MOD_NUM_LOCK  0x0020

@kovidgoyal
Copy link
Contributor Author

I vote for option 1 on the grounds that it is less likely to cause compatibility issues with code that checks the masks incorrectly.

@elmindreda
Copy link
Member

You read my mind. Option 1 it is.

@elmindreda elmindreda removed their assignment Oct 26, 2017
@refi64
Copy link

refi64 commented Nov 12, 2017

FWIW seeing this in GLFW 3.3 would be amazing!!

@elmindreda elmindreda self-assigned this Nov 30, 2017
@elmindreda elmindreda added this to the 3.3 milestone Nov 30, 2017
@elmindreda elmindreda added macOS Mir Wayland Windows Win32 specific (not Cygwin or WSL) X11 labels Nov 30, 2017
elmindreda added a commit that referenced this issue Dec 5, 2017
This adds the GLFW_MOD_CAPS_LOCK and GLFW_MOD_NUM_LOCK modifier bits.
Set the GLFW_LOCK_KEY_MODS input mode to enable these for all callbacks
that receive modifier bits.

Fixes #946.
elmindreda added a commit that referenced this issue Dec 5, 2017
This adds the GLFW_MOD_CAPS_LOCK and GLFW_MOD_NUM_LOCK modifier bits.
Set the GLFW_LOCK_KEY_MODS input mode to enable these for all callbacks
that receive modifier bits.

Fixes #946.
@elmindreda
Copy link
Member

The modifier bit path was so much cleaner internally and conceptually simpler externally (no potentially out-of-date cached state) that I had to give it a try.

What do you think of f3de10d?

@kovidgoyal
Copy link
Contributor Author

Fine by me.

@AndrewBelt
Copy link
Contributor

AndrewBelt commented May 17, 2018

I'd argue that this is extremely rare

I'm another guy who needed it for https://vcvrack.com/ to implement a togglable "virtual computer keyboard MIDI device" when Caps Lock is enabled. This is how most DAWs do it. Much thanks, and good API choice!

@elmindreda
Copy link
Member

@AndrewBelt Good to know, thank you!

@AngelDeaD
Copy link

@elmindreda is this also released, like #1098 ?

@elmindreda
Copy link
Member

@AngelDeaD Sort of. The 0e8c4ea patch above is merged and if you turn lock key modifiers on (see GLFW_LOCK_KEY_MODS) then you can tell them apart by GLFW_MOD_NUM_LOCK. Having actual separate symbols for the different uses of the same physical keys has to wait until 4.0.

m4ce-w1ndu pushed a commit to m4ce-w1ndu/glfw that referenced this issue Jul 23, 2022
This adds the GLFW_MOD_CAPS_LOCK and GLFW_MOD_NUM_LOCK modifier bits.
Set the GLFW_LOCK_KEY_MODS input mode to enable these for all callbacks
that receive modifier bits.

Fixes glfw#946.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature suggestions and PRs macOS Mir Wayland Windows Win32 specific (not Cygwin or WSL) X11
Projects
None yet
Development

No branches or pull requests

6 participants