Virtual-key Code

0 views
Skip to first unread message

Antígona Knknown

unread,
Aug 5, 2024, 1:39:18 AM8/5/24
to ovunmidfi
Isuggest hooking the WM_SYSKEYDOWN messages, and noting that the values correspond to constants that more often have identifiers (VK_MENU, in this case). After all, as programmers we're taught early on not to use magic numbers!

Another option, as pointed out in the comments, is to use GetAsyncKeyState. Your concerns that you won't be able to use vkCode seem strange, as GetAsyncKeyStates parameter is the virtual key code, after all, so the code you'd be using would be along the lines of:


NSEvent keyCode gives a keyboard scan code, which is a hardware specific code representing the physical key. I want to convert the scan code to a virtual key code, which is the logical key based on the users keyboard layout (QWERTY, AZERTY, etc).


For example, kVK_ANSI_A (from Carbon/HIToolbox/Events.h, value 0x00) does not mean the key which produces the 'A' character, it means the key which is in the position that the 'A' key is in an ANSI standard keyboard. If a French keyboard layout is active, that key will produce 'Q'. If the physical keyboard is a French keyboard, that key will probably be labeled 'Q', too.


To translate from the virtual key code to a character, you can use UCKeyTranslate(). You need the 'uchr' data for the current keyboard layout. You can get that using TISCopyCurrentKeyboardLayoutInputSource() and then TISGetInputSourceProperty() with kTISPropertyUnicodeKeyLayoutData as the property key.


You also need the keyboard type code. I believe it's still supported to use LMGetKbdType() to get that, even though it's no longer documented except in the legacy section. If you don't like that, you can obtain a CGEvent from the NSEvent, create a CGEventSource from that using CGEventCreateSourceFromEvent(), and then use CGEventSourceGetKeyboardType()and call CGEventGetIntegerValueField() with kCGKeyboardEventKeyboardType to get the keyboard type.


Of course, it's much easier to simply use -[NSEvent characters] or -[NSEvent charactersIgnoringModifiers]. Or, if you're implementing a text view, send key-down events to -[NSResponder interpretKeyEvents:] (as discussed in Cocoa Event Handling Guide: Handling Key Events) or -[NSTextInputContext handleEvent:] (as discussed in Cocoa Text Architecture Guide:Text Editing). Either of those will call back to the view with the appropriate action selector, like moveBackward:, or with -insertText: if the keystroke (in context of recent events and the input source) would produce text.


**) "mouse button" quasi VK codes are generated by Windows when user clicks left/middle/right/X1/X2 mouse buttons. KbdEdit allows these codes to be assigned to physical keys, but such mappings make no sense: pressing a key with VK_LBUTTON assigned to it will NOT make Windows act as if physical mouse button was pressed.


Requires Windows Vista or later. These can be used as hotkeys with some (but not all) mice which have a second wheel or support tilting the wheel to either side. In some cases, software bundled with the mouse must instead be used to control this feature. Regardless of the particular mouse, Send and Click can be used to scroll horizontally in programs which support it.


Although any single character can be used as a key name, its meaning (scan code or virtual keycode) depends on the current keyboard layout. Additionally, some special characters may need to be escaped or enclosed in braces, depending on the context. [v1.1.27+]: The letters a-z or A-Z can be used to refer to the corresponding virtual keycodes (usually vk41-vk5A) even if they are not included in the current keyboard layout.


Due to system behavior, the following keys separated by a slash are identified differently depending on whether NumLock is ON or OFF. If NumLock is OFF but Shift is pressed, the system temporarily releases Shift and acts as though NumLock is ON.


Specify for nn the hexadecimal virtual key code of a key. This rarely-used method also prevents certain types of hotkeys from requiring the keyboard hook. For example, the following hotkey does not use the keyboard hook, but as a side-effect it is triggered by pressing either Home or NumpadHome:


Joy1 through Joy32: The buttons of the controller. To help determine the button numbers for your controller, use this test script. Note that hotkey prefix symbols such as ^ (control) and + (shift) are not supported (though GetKeyState can be used as a substitute). Also note that the pressing of controller buttons always "passes through" to the active window if that window is designed to detect the pressing of controller buttons.


Multiple controllers: If the computer has more than one controller and you want to use one beyond the first, include the controller number (max 16) in front of the control name. For example, 2joy1 is the second controller's first button.


Reconfigure the software that came with your mouse or keyboard (sometimes accessible in the Control Panel or Start Menu) to have the "mystery key" send some other keystroke. Such a keystroke can then be defined as a hotkey in a script. For example, if you configure a mystery key to send Ctrl+F1, you can then indirectly make that key as a hotkey by using ^F1:: in a script.


The following is a last resort and generally should be attempted only in desperation. This is because the chance of success is low and it may cause unwanted side-effects that are difficult to undo:

Disable or remove any extra software that came with your keyboard or mouse or change its driver to a more standard one such as the one built into the OS. This assumes there is such a driver for your particular keyboard or mouse and that you can live without the features provided by its custom driver and software.


Some configurations of Windows IME (such as Japanese input with English keyboard) use CapsLock to toggle between modes. In such cases, CapsLock is suppressed by the IME and cannot be detected by AutoHotkey. However, the Alt+CapsLock, Ctrl+CapsLock and Shift+CapsLock shortcuts can be disabled with a workaround. Specifically, send a key-up to modify the state of the IME, but prevent any other effects by signalling the keyboard hook to suppress the event. The following function can be used for this purpose:


I don't want to faff around with the shortcut of "ALT key and type the number 0163" to obtain the symbol. Instead I just want to use something like Right Alt + 4 to obtain it. Microsoft Powertoys allows you to remap keys. However, it doesn't seem to allow a symbol in the "Mapped to" section.


I'm therefore wondering if I can use a "virtual key" code instead which might produce the symbol. How would I determine this code number? I found this link, but no luck with any pound symbol. Or maybe there's something better and more logically designed than PowerToys that gives a much more diverse set of characters?


Don't confuse keys with characters. Which characters are sent will depend on keyboard character mapping. You need two key presses to access the underscore character. There are possibly other ways to determine the resulting character, which I don't know about, but testing two keys is probably (still) easier.


Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."


I have a range of 7 keystrokes and i am reading them. I have given some names to keystrokes so they return me a string. example: 1 returns Paul, 2 return Lio etc. I am using this to add some features on a project i have created for my job and to replace an inputbox which i used to use to get the name. My problem is that some of the characters i use require double keystrokes...


Are you using _IsPressed(10) to return Paul? You give an example of a range of keys and then say you are having problems with the underscore character. This doesn't make sense to me. Underscore is not a keystroke in itself - as stated previously. There is also no key with Paul written on it. The question is too vague and the problem is consequently unclear (to me at least).


check on a sketch of mine.

my values are in white, and there is only one space between value name and its value.

the sketch is working (register adress for I2C protocol, so any issue in mapping would break the code)


The following table shows the symbolic constant names, decimal values (in brackets), and mouse or keyboard equivalents for the virtual-key codes used by the system. The codes are listed in numeric order.


I'm developing a Keyboard for a Touch Screen Windows 7 application and I need to have access to Special Characters such as $ % ^ and others but I can't find a way to use them from the Windows Virtual Keyboard. I'm using the following example which has all regular characters you can find on a keyboard.


I read the MSDN documentation and I understand that VK Codes reffer to keybord keys and not to keyboard characters, so if you need use a character that require combine more than one key you need code the combination that provide the disired character.


The virtual-key codes identify various virtual keys.Virtual keys mainly consist of actual keyboard keys, but also include "virtual"elements such as the three mouse buttons. The virtual keys also include many"keys" which usually do not exist at all. A key's virtual-key codedoes not change when modifier keys (Ctrl, Alt, Shift, etc.) are held -- e.g.,the 1 key has the same virtual-key code whether 1 or ! is pressed. However, thenumbers in the numeric keypad on the keyboard do have two different virtual-keycodes: one for when Num Lock is on, and another for when Num Lock is off. Notethat the virtual-key codes of 0-9 and A-Z equal their ASCII codes.


Note: The actual meanings of some of the key codesmay vary on keyboards designed for different languages. Most notably, the VK_OEM_*that denote punctuation keys may vary between languages, relating to a differentpunctuation key. The meanings listed below are for a U.S. English-languagekeyboard.

3a8082e126
Reply all
Reply to author
Forward
0 new messages