Using Numpad with Modifier Keys exhibits curious behavior - windows

I have a keyboard event listener, and I am listening for the number pad key codes (1 through 9) for when number lock is activated; this works fine. However, in my app I also want to allow usage of a modifier key (CTRL) along with the number pad keys. The strange thing is that when holding CTRL, pressing 1 or 3 does not generate any keyboard event whatsoever, whereas 2 and 4 - 9 do generate the expected events. I have seen other references to this issue after some Googling, so I do not believe this is necessarily Flash-specific, but I have yet to find any answers.
I tried using SHIFT as a modifier, but that just results in generating the key codes from the number pad as if number lock was off (eg, SHIFT+Numpad1 returns the End keycode, regardless of number lock state) - apparently this is intentional Windows behavior. ALT is not an option with the numpad due to altcodes.
Any ideas on how to get CTRL+Numpad1 and CTRL+Numpad3 to generate the keyboard events? Or any explanation as to why they don't?
Edit: I tried out using these key combinations in Firefox as enriquein suggests below, and all the key combinations work A-OK, leading me to believe that this is likely a Flash-specific issue, or at least not a hardware issue.

I have run into issues in the past with numpad keys on specific keyboards. Various key combinations fail to register on specific keyboards, and it is possible that the keyboard you are using may just not be generating any event in that case. It's not guaranteed to work, but I would recommend trying out a different keyboard (different vendor, etc) and see if that works. It also might be an issue with localized keyboards, if you are using a non-english keyboard.

I'm almost certain it's not Flash related because I had similar problems with a localized keyboard and could not get it to generate events at all for certain keys or key combinations.
Try enumerating all key events and searching trough what they're mapped to, or google evtest.c, compile it and run it and see what it has to say.

Indeed it looks like its keyboard specific. I tried the key combinations as I read this question in Firefox and they triggered the same event as pressing Ctrl+Number (which is switch to tab #Number).
This was using a standard US/101-key english keyboard (no extra media buttons or anything).

Some keyboards don't register certain key combinations. I think that this is down to how they are put together. For example some may register left hand Ctrl + key combination but not right Ctrl + same key!
A work around would be to detect the key down and key up events for the Ctrl key.
You could then raise a custom event for Ctrl + numpad key or work with a volatile flag to show the Ctrl key state.

Related

Standard (no third-party) way to get numeric keypad working as arrow keys on macOS (Xcode? hidutil?)

I always use numeric keypad as arrows, as I find it more convenient than using an separate arrow pad. Until recently I used a tool Karabiner Elements, but it stopped functionning at Big Sur.
Yes, The Karabiner developer is working on that issue, but it will be better to solve it without relying on a third-party tool.
It will be enough for me to either remap Xcode, or (which is preferred) to change key bindings in the system, using a tool like hidutil.
Xcode's Preferences - Key Binding has a section Text - Section. When I try to modify the binding for say Move Down by pressing Down arrow on numeric keypad it comes up as '2'. Now whereever I press '2', on keyboard or numeric keypad, it always works as Move Down which is certainly not what I am after.
About hidutil. I couldn't find a good manual or any example of keyboard mapping.
Any help?
Had another look at this article. Here is the answer:
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x70000005A,"HIDKeyboardModifierMappingDst":0x700000051},
{"HIDKeyboardModifierMappingSrc":0x700000060,"HIDKeyboardModifierMappingDst":0x700000052},
{"HIDKeyboardModifierMappingSrc":0x70000005C,"HIDKeyboardModifierMappingDst":0x700000050},
{"HIDKeyboardModifierMappingSrc":0x70000005E,"HIDKeyboardModifierMappingDst":0x70000004F},
{"HIDKeyboardModifierMappingSrc":0x70000005F,"HIDKeyboardModifierMappingDst":0x70000004A},
{"HIDKeyboardModifierMappingSrc":0x700000061,"HIDKeyboardModifierMappingDst":0x70000004B},
{"HIDKeyboardModifierMappingSrc":0x700000059,"HIDKeyboardModifierMappingDst":0x70000004D},
{"HIDKeyboardModifierMappingSrc":0x70000005B,"HIDKeyboardModifierMappingDst":0x70000004E},
{"HIDKeyboardModifierMappingSrc":0x700000062,"HIDKeyboardModifierMappingDst":0x700000049},
{"HIDKeyboardModifierMappingSrc":0x700000063,"HIDKeyboardModifierMappingDst":0x70000004C}]}'
Still can't fugure out, how to do (if possible) complex mapping with modifier keys (eg Ctrl+PgUp to Home), but that's another topic.

Hotkey field in XUL for Firefox

I have a Firefox extension written in XUL. It takes a hotkey. I want to have an input field where the user can press any key and I can programmatically determine what key was pressed.
I don't care about key combos which have meta meanings inside the OS (I don't need to capture Ctrl + Alt + Del).
If the key is already defined by Firefox, I'm OK with not capturing it (for instance, if the user presses F1 and help pops up instead of the key being captured, I'm fine).
I would also love to have some way to determine programmatically whether the key pressed already has some other meaning inside of Firefox, but that is likely out of scope for this request. I just mention it in case it's easy.
My current solution, which seems to have been adopted by a number of add-ons, is to provide a text box where the user can type a printable character and then a series of check boxes for modifier keys. This solution is barely workable but terrible for a number of reasons (it doesn't allow users to use non-printing keys such as function keys in their combos, it allows the user to input invalid characters (such as Unicode characters with no physical key on the keyboard), and it's just awkward to use).
Thanks!
You can use Inline Options
https://developer.mozilla.org/en-US/Add-ons/Inline_Options
You can detect the key pressed in the text input and write your modifiers like accel or alt.
Source example:
http://git.io/vez1o

Remap Caps lock key to Esc in Mma 7

TLDR: How do I get CapsLock to translate to "ShortNameDelimiter" in Mma 7?
I like pretty text in my mma notebooks, and often define functions as f[\[Alpha]_] =... so as to match the exact equation that I'm working with. As such, it involves a lot of Esc-letter-Esc sequences, and reaching for Esc every other stroke breaks my flow of typing.
Now, the CapsLock key is seldom used (I can't remember the last time I needed it), but conveniently placed (your pinky is right there!). Remapping it to Esc on vim worked wonders for me and I was wondering if there was a way to do the same in mma, without having to modify the system's keyboard layout.
I tried editing KeyEventTranslations.tr by adding the following in EventTranslations[{...
Item[KeyEvent["CapsLock"], "ShortNameDelimiter"]
but that had no effect. Is there another way to do it? Is CapsLock not the correct identifier? If it helps, I'm using Mma7 student version on a Mac.
Modifier keys are handled quite specially, and I doubt Mathematica will be able to override the system. You probably have to do this in a layer between Mathematica and the OS. BUT, it is possible to make the key behave different depending on the application you are in. Thus with a bit of work, it MAY be possible to have the capslock key behave differently only in Mathematica.
edit: I did not see you say which operating system you had, so I've added Mac instructions.
Windows
For example, if you have Windows, you can use the program called http://www.autohotkey.com/ . It specifically has a feature where you can bind a key to a script, specifically the following script:
How can a hotkey or hotstring be made exclusive to certain program(s)?
In other words, I want a certain key to act as it normally does except when a specific window is active.
In the following example, NumpadEnter is made to perform normally except when a window titled "CAD Editor" is active. Note the use of the $ prefix in "$NumpadEnter", which is required to let the hotkey "send itself":
$NumpadEnter::
IfWinNotActive, CAD Editor
{
Send, {NumpadEnter}
return
}
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
This next example is more pure than the above, but it will only work if the "CAD Editor" application is designed to ignore the NumpadEnter key itself. The tilde prefix (~) makes NumpadEnter into a non-suppressed hotkey, meaning that the NumpadEnter keystroke itself is always sent to the active window, the only difference being that it triggers a hotkey action. The ~ feature requires Windows NT/2k/XP.
~NumpadEnter::
IfWinNotActive, CAD Editor
return
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
To quote from "MRCS" in this forum post, you may find the following useful:
The first one I named CapsLockR.ahk and contains the following script:
CapsLock UP::Run C:\Documents and Sett...[path to script]...\CapsLock.ahk
The second one is named CapsLock.ahk and has this script:
GetKeyState, state, CapsLock, T
if state = D
SetCapsLockState, off
else
SetCapsLockState, on
exit
Thus worse comes to worst, if you are having trouble modifying the "Behave like Foo if Active Window = Mathematica else behave like Bar" script, you can tack on this to manually toggle the CapsLock state I think. Googling will also reveal more results.
Linux
I know that on Linux, you can use the program called xbindkeys to bind the CapsLock to a script, from which you can in turn call xdo if you detect Mathematica is one of the topmost windows (e.g. via Getting pid and details for topmost window , or xdotool getwindowfocus) or worse-comes-to-worst, you can just have a script which toggles your configuration between CapsLock -> xdotool key Escape, xdotool type "whatever", xdotool key Escape ("Mathematica mode") and "normal mode"... though that may prevent you from YELLING AT MATHEMATICIANS OVER INSTANT MESSAGING WHILE DOING MATHEMATICS. Unless you You may need to find some way to programatically toggle CapsLock, perhaps by creating a dummy CapsLock key (though that's an extreme hack, it is likely one can find some kind of library; perhaps Anybody know how to toggle caps lock on/off in Python? may be useful). (This issue could be avoided by using a key besides CapsLock, or not caring that you want to keep your CapsLock functionality; you could also just turn another key you never use into CapsLock.)
Mac
Mac may have similar tools. For example, you can get xdotool like on Linux above via the MacPorts project. I hear the CapLock key cannot normally be rebound as easily on Mac, so if you can deal with another key it may be much easier. But theoretically it should be possible...
If you wish to use CapsLock, you can use PCKeyboardHack http://pqrs.org/macosx/keyremap4macbook/extra.html to remap the CapLock key to something which will tell OS X to let you remap the CapsLock. Then you remap it, then bind the key using Quicksilver to a script that makes calls xdotool to check if you're in Mathematica also also to issue the :esc:...:esc: if you are (see the Linux section of this answer). Otherwise you simulate a keypress on the CapsLock. But you remapped CapsLock! So you might need to make another dummy key you never use into the CapsLock key, and trigger a keypress on that using Cocoa libraries or a simple AppleScript. If you wish to pursue the CapsLock route, you might find Using Caps Lock as Esc in Mac OS X useful.

Remap keyboard combinations, not just single registry values

I'm getting real tired of pressing shift every time I want to type $ or #. I've checked out key remapping programs like SharpKeys and a few others, but they don't support the SHIFT+[key] actions. The problem is they modify the registry value for a single key only (I think).
I want to flip over my number keys so when I type 4, it outputs $, similar to a French keyboard. (I tried physically rotating the key so it's upside down, but that's not working).
There must be a solution for this - can you help?
The registry hackers move virtual keys around. What you need is a custom keyboard layout. You can easily create your own with the Microsoft KeyBoard Layout Creator utility.
In addition to the answer from #HansPassant, if you find trouble with the ctrl+KEY functions etc you might need to edit the .klc file manually.
See https://superuser.com/questions/171307/how-can-i-restore-ctrlkey-functionality-in-microsoft-keyboard-layout-creator :
You need to modify the .klc file manually.
Basically you just modify the VK_ column to match the value in
column 1.
So for example if you want to bind L to N, you
would create the keyboard as you normally would in KLC. Then you would
open the KLC file in a text editor. Find the value L in the VK_
column, and switch it to an N.
For more information, I wrote the complete steps on my blog.

How to determine if a certain key is pressed, knowing only its position on U.S. keyboards?

Consider that, for a Windows video game, I need to determine if the key which generates the ` and ~ characters on the U.S. English keyboard layout (which is usually below the Escape key and left to 1) has been pressed. This may sound like a trivial question, but it doesn't seem like one to me.
When Windows sends keyboard messages, it specifies the virtual key code and the OEM scan code. We can't rely on the OEM scan code, because "the value depends on the OEM" - and nor can we depend on the virtual key code, because it depends on the currently active keyboard layout.
Our current "solution" is to use LoadKeyboardLayout and MapVirtualKeyEx to find the OEM scan code of the key that generates the ` character on the U.S. English keyboard layout, then just listen for that OEM scan code. The problem is that this doesn't work if the user doesn't have the U.S. English layout installed.
Is there a real way to do this on Windows?
The OEM scancode does not change from keyboard to keyboard. No reason not to use it.
Back in the DOS-days the same scancodes have been used for games because it has been the only way to detect key-up and key-down events. Noone had problems with it and I doubt it will change in the future.
If you want another option you may want to give DirectInput-API a try. It gives you the raw scancodes as well and if I'm not mistaken you can also query the physical position, dimension and whatnot of each key.
Tie the game action to the character, not the position of the key. Otherwise, how do you tell the user which key to press? "under the escape key"? They may not have anything there, but if you tell them "the ^ key", they can look for it.
You'll probably also want to make it configurable to accomodate exotic keyboard layouts and user preferences.

Resources