Converting a VK_CODE into a displayable string - c++

When writing a Windows application, the documentation says that some VK_CODEs are displayable characters, like VK_OEM1 is "o with an umlaut". How can I go from the WPARAM of non-ASCII characters into a displayable string? I'm using UTF-16.

Maybe you're looking for the GetKeyNameText Function
It retrieves a string that represents the name of a key.

like VK_OEM1 is "o with an umlaut".
Maybe on your machine. Not on mine, it is ';' or ':', depending on the Shift key state. These are virtual key codes. The ones that represent a typing key get converted to a character by ToUnicodeEx(), a function that takes a keyboard layout. And of course you have the non-typing keys that produce no character at all, like VK_F1 or VK_NUMLOCK. This gets a lot more complicated when the keyboard layout has dead keys, the kind you use to get a diacritic on top of a character. That why the function also requires a keyboard state.
Avoid this like the plague, WM_CHAR is your friend.

Related

Virtual-Key Codes for special characters

I am trying to get the Virtual-Key Code for another characters out of regular virtual-key codes list, like '<', but I cant find it, it's an ASCII character and I can't find it.
“ASCII” is a character encoding.1 This has very little to do with key codes. For example, “A” and “a” have distinct ASCII values but they are represented by the same key on the keyboard (plus a modifier key — shift).
That’s the reason why you don’t find < in the list — it’s not a key on the (virtual) keyboard represented by these key codes. It depends on the currently active keyboard mapping how this character is represented by by a keycode.
1 And, I might add, not a very relevant one in this day and age. Better forget about ASCII, it’s mostly misused.
Thanks to Hans Passant
VkKeyScanEx() might help.
For example, the Virtual-Key Code equivalent to '<' can be found with:
VkKeyScanEx('<', GetKeyboardLayout(0))

Using multi key sequenced inputs to result in a function

Straight to the point. I've been using visual studio to run a game I've been making. So far while coding in C++ I've been using inputs like VK_BACK or 0x52 for single key inputs. But I currently need to be able to type a word while the game is running to trigger a function. Basically, I'm asking how you would go about making the program recognize an inputted word. Like pressing the keys in a sequence to trigger a function.
Thanks, Jack
You've left a lot up to us to guess:
What are you using to read keyboard input?
I assume you're polling rather than streaming?
I'm also assuming non-blocking input?
With so many unknowns it's difficult to give a useful answer to the question. But here are a handful of options that may be helpful to you:
Require a terminating character for all keyboard input. This is characteristic of blocking input. The input is only read when the terminating character is placed.
Require the holding of a mode key while inputting a string. For example if I press an 'a' the key's command is executed immediately, but if I'm currently holding down the left Ctrl key and I press an 'a', it is treated as being within a string until the Ctrl key is released.
Use delimiters. If the '\'' is pressed it denoted the beginning of a string input consisting of all characters till the next '\'' key is pressed.
Make string entry keys separate from instantaneous input keys. This may be the most convenient solution because it allows instantaneous input in the middle of string input. But it may also be frustrating to the user if the allowable string commands are not clearly defined.

C++ vkCode/ScanCode to country-specific and case-sensitive character

I searched in a lot of different places and didn't find any conclusive answer, so I'm asking here.
I have to make a basic Windows keylogger as a school project.
I managed to set it up, and it works quite well. It logs characters and dead keys.
My problem is handling case-sensitive and country-specific input. I can get the SHIFT key state just fine to convert keys into their upper/lowercase equivalent, but I can't actually know what upper/lowercase equivalent it corresponds to.
Example : I'm using a French AZERTY keyboard. Numbers can be written via SHIFT + &, SHIFT + é, etc.
Here are the results my keylogger displays when trying to log numbers 1 to 5, first via SHIFT + Number and then by using CAPS LOCK.
If I switch to an English QWERTY keyboard, it will probably work fine, displaying the numbers (but then I'll not be able to access other characters like parentheses).
Is there any way to get a list of all possible keys for a given ScanCode/vkCode, depending on the type of keyboard ?
Here's a part of my code for the Callback function, and the translation of keys to strings.
It's the first time I use Windows' C++ API, so I don't really know much about it.

Does CComboBox control always takes capital letters as we type in it

I am using CComboBox control. When I type some characters in it and check which letter is typed in(in PreTranslateMessage()), then I always get capital letter in its message's wParam. My CComboBox control does not have uppercase property TRUE. Why this is happening?
Keys are funny things. What's the default state, lowercase or uppercase?
If you look at your keyboard, most likely the physical keys have uppercase letters on them. Default: uppercase
When you type in keys, you need to hold the shift key to create upper keys, without the shift keys you get lower case. Default: lowercase
As an alternative, you can use the Caps Lock key. Caps Lock is normally off . Default: lowercase.
The untranslated key presses sent to your application use VK_A - VK_Z keycodes. VK_A is 'A' not 'a'. Default: uppercase. Caps lock and shift are applied later, in translation.
This could have been designed consistently, but it wasn't, and now we're stuck with the mess to be backwards compatible. If you want the "normal" keyboard behavior, don't try to exactly replicate the OS behavior. There's stuff like "Sticky Keys" (hold shift to get Caps Lock-like behavior) which you might not even know. Instead, use the end result from the OS. For Windows, that's WM_CHAR.

Can all keys be represented as a single char in c++?

I've searched around and I can't seem to find a way to represent arrow keys or the escape key as single char in c++. Is this even possible? I would expect that it would be similar to \t or \n for tab and new line respectively. Whenever I search for escaped characters, there's only ever a list of five or six well known characters.
The short answer is no.
The long answer is that there are a number of control characters in the standard ANSI character set (from decimal 1 to decimal 31, inclusive), among which are the control codes for linefeed, carriage return, end-of-file, and so on. A few are commonly interpreted as arrows and the escape key, but only for compatibility with terminals.
Standard PC keyboards send a 2- or 3-byte control code that represents the key that was pressed, what state it's in, which control/alt/shift key is pressed, and a few other things. You'll want to look up "key codes" to see how to handle them. Handling them differs between operating systems and the base libraries you use, and their meaning differs based on the operating system's configured keyboard layout (which may include characters not found in the ANSI character set).
Not possible; keyboards built for some languages have characters that can't be represented in a char, and anyway, how do you represent control-option-command-shift-F11 in a char?
Keyboards send scancodes, which are either some kind of event in a GUI system or a short string of bytes that represent the key. What codes depends on your system, but on most terminal-like systems, ncurses knows how to deal with them.
char variables usually represent elements in the ASCII table.
http://www.asciitable.com/
there is also man ascii on unix. If you want arrow keys you'll need a more direct way to access keyboard input. the arrow keys get translated into sequences of characters before hitting stdio. If oyu want direct keyboard access consider a GUI library, sdl, direct input to name a few.
There aren't any escape characters for the arrow keys. They are represented as Keycodes, afaik. I suggest using a higher level input library to detect key presses. If you want to do it from scratch, the approach taken might vary with the specific platform you are programming for.
In any case, back in the days of TURBO C, I used -
//Wait for keypress
//store input in ch
//see if the ASCII code matches the code for one of the arrow keys