WxWidget: understanding if key at left of "1" has been pressed - c++

Is there a somehow portable way to understand if the key at left of "1" on the top raw of the keyboard has been pressed, by analyzing a wxKeyEvent?
For that key, in my keyboard both GetRawKeyCode() and GetKeyCode () return 126, which is 0x7E, and which seems to correspond to what I read here, but I don't know if it is portable to "any" (a good majority) of keyboards.
The rationale behind: my window react by pressing 0, 1, 2, 3, and I want that the key at the left of 1 gives the same behaviour of 0.

There is no portable way to do it (of course, all keyboards may not even have a key to the left of "1"). And I don't think raw key code is a good way to identify this key even under Windows as it will be different if you use non-US keyboard layout. I'd probably use the bits 16-23 of the raw key flags which contain the scan code there. Raw key flags should also work with wxGTK as they contain the hardware_keycode there. I am not sure about OS X, the raw flags there are just the modifiers and I don't know if the key code is layout-independent.

Related

I need ASCII key code for Shift key. All the numbers I've found on the internet doesn't work

I'm using C++, getch() function. I've done some research and they said that it's 14, 15 or 16, 17. But none work. Key code for up and down arrow (72 and 80) work fine though.
ASFAIK there is no ASCII code for the shift key. You can refer to the ASCII table found here.
I think what you are being confused about is that in the ASCII table there are values of: 14 and 15 or E & F in hex. These have the properties of shift in and shift out which were used many years ago which are rarely used any more and many of the first set of numbers aren't even used for their original purposes.
However, in <WinUser.h> header file; there is a bunch of defines for many different keys and where the Shift Key is concerned it is defined as such:
#define VK_SHIFT 0x10
However; if you are writing a function to handle keyboard input through key presses and releases using a Window's messageHandler() function to get Windows Messages; you can not use VK_SHIFT directly because you have to do bit manipulation to test if it is the Left Shift or the Right Shift. This is determined by the bits that were set.
Here is what a possible if statement would look like when querying for the shift key...
if ( (wParam == VK_SHIFT) && ((lParam & 0x360000) == 0x360000) ) {
wParam = VK_RSHIFT;
}
Other than that; I do not know exactly what you are trying to ask.
// you can implement the shift key in your custom keyboard by doing like this
<Row>
<Key android:keyWidth="14.275%p" android:horizontalGap="1%p"
android:codes="-1" android:keyEdgeFlags="left" android:isModifier="true"
android:keyIcon="#drawable/ic_shift_normal"/>
<Key android:keyWidth="14.275%p"
android:codes="-5"
android:keyEdgeFlags="right"
android:isModifier="true"
android:isRepeatable="true"
android:keyIcon="#drawable/ic_backspace"/>
</Row>

Get any key from keyboard in C++ including keys like control

I'm programming in C++ and have run into a wall.
I need to get input from the keyboard. The problem is that I also need to get input from keys like control, scroll lock, windows key, etc. I also need to be able to differentiate between the numpad and regular numbers 0-9.
I tried using _getch(). While it can get keys like arrow keys and the numpad, I can't get keys like control, shift and scroll lock.
Does anyone have any suggestions?
There is no standard way to do this because C++ does not assume the system even has all those things.
A good solution for what you are trying to do is the SDL library. Look here:
https://www.libsdl.org/
I see the word "windows key" so I'm assuming you're programming for Windows
Use WinAPI ReadConsoleInput
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD ir;
DWORD read;
if (!ReadConsoleInput(hInput, &ir, 1, &read) || read == 0) {
// Something went wrong
}
if (ir.EventType == KEY_EVENT) {
// Do stuff here
}
Refer to KEY_EVENT_RECORD for more information. You can get control keys states from
ir.Event.KeyEvent.dwControlKeyState
This is an example provided by Microsoft.

Detecting keyboard hotkeys when DirectInput DIK constants fail

I want to assign tasks and functions to the additional keys of my keyboard (e.g. Mute,VolumeChange, Browser,...). Now, I do know that DirectInput implements a full list of those keys like DIK_MUTE (msdn link). Unluckily, none of the keys are assigned to those values and obviously every key pressed is recognized as No. 128 by DirectInput.
But Windows seems to have no problem distincting between those keys as some of them are functional and for instance open applications. Is there a way to tweak this issues with or without DirectInput so that i can detect those keys also in fullscreen DX/OpenGL applications?
IDirectInputDevice8* device;
BYTE arrKey[256] = {0};
if FAILED(device->GetDeviceState(sizeof(BYTE)*256,arrKey))
return false;
BYTE byKey = ReportPressedKey(arrKey, sizeof(BYTE*)*256);
// byKey = 128 (DIK_MUTE, DIK_VOLUMEUP,DIK_NEXTTRACK,...)

Handling keyboard input in win32, WM_CHAR or WM_KEYDOWN/WM_KEYUP?

So in the text editor program that i've been working on, I've used WM_CHAR to process input from the keyboard. However, I found that some of the character mesages are not recorded. For example, if I use [shift]+ number key to type a symbol such as % or &, some re recorded while others such as [shift]+9 (which results in ')'), are not recorded. So, I'm wondering if I should use WM_KEYDOWN/WMKEYUP pair to handle keyboard input. I once wrote a keylogger in assembly(actually it was just a tutorial that i was trying out) and had used WM_KEYDOWN/WM_KEYUP pairs and that worked out quite good. So, should I move on to this, or is it something unusual that is happening with my program?
Thanks,
Devjeet
This is really a long reply to your comment above, but putting it in an answer because it's too long for a comment :)
The core issue to understand here is that keys and characters are not quite the same thing. Some (but not all) keys generate characters; some keys generate different characters depending on shift or other keyboard state. And to implement an editor, you need to handle both textual input and also non-textual keyboard input like arrow keys. Now the long-winded version, picking off from what seems to be an incorrect assumption:
Apparently, windows works in really strange ways. [...] It seems that when you press [shift]+9, windows sends a VK_LEFT in the wParam of message WM_CHAR
Sounds like you might be mixing two things up here. The thing with WM_CHAR is that it gives you character codes for textual characters: so if someone presses the 9 key, you'll get '9'. If someone presses SHIFT+9, Windows will take the shift state into account - and you get '(' (if using US keyboard). But you won't ever get a WM_CHAR for arrow keys, HOME, END, and so on, since they are not textual characters. WM_KEYDOWN, on the other hand, does not deal in characters, but in VK_ codes; so pressing 9 gives you VK_9 regardless of shift state; and left arrow gives you VK_LEFT - again regardles of shift state.
The things is that WM_CHAR and WM_KEYDOWN both give you two parts to the overall input picture - but you really have to handle both to get the full picture. And have to be aware that the wParam is a very different thing in both cases. It's a character code for WM_CHAR, but a VK_ code for WM_KEYDOWN. Don't mix the two.
And to make things more confusing, VK_ values share the same values as valid characters. Open up WinUser.h (it's in the include dir under the compiler installation dir), and look for VK_LEFT:
#define VK_LEFT 0x25
Turns out that 0x25 is also the code for the '%' character (see any ascii/unicode table for details). So if WM_CHAR gets 0x25, it means shift-5 was pressed (assuming US keyboard) to create a '%'; but if WM_KEYDOWN gets 0x25, it means left arrow (VK_LEFT) was pressed. And to add a bit more confusion, the Virtual Key codes for the A-Z keys and 0-9 keys happen to be the same as the 'A'-'Z' and '0'-'9' characters - which makes it seem like chars and VK_'s are interchangable. But they're not: the code for lower case 'a', 0x61, is VK_NUMPAD1! (So getting 0x61 in WM_CHAR does mean 'a', getting it in WM_KEYDOWN means NUMPAD1. And if a user does hit the 'A' key in unshifted state, what you actually get is first a VK_A (same value as 'A') in WM_KEYDOWN, which gets translated to WM_CHAR of 'a'.)
So tying all this together, the typical way to handle keyboard is to use all of the following:
Use WM_CHAR to handle textual input: actual text keys. wParam is the character that you want to append to your string, or do whatever else with. This does all the shift- processing for you.
Use WM_KEYDOWN to handle 'meta' keys - like arrow keys, home, end, page up, and so on. Pass all the A-Z/0-9 values through, the default handling will turn them into WM_CHARs that you can handle in your WM_CHAR handler. (You can also handle numpad keys here if you want to use them for special functionality; otherwise they 'fall through' to end up as numeric WM_CHARs, depending on numlock state. Windows takes care of this, just as it handles shift state for the alphabetic keys.)
If you want to handle ALT- combos explicitly (rather than using an accelerator table), you'll get those via WM_SYSKEYDOWN.
I think there are some keys that might show up in both - Enter might show up as both a WM_KEYDOWN of VK_RETURN and as either \r or \n WM_CHAR - but my preference would be to handle it in WM_KEYDOWN, to keep editing key handling separate from text keys.
Spy++ will show you the messages being sent to a window, so you can experiment and see what messages are appropriate for your application.
If you have Visual Studio installed, it should be in your Start menu, under Programs -> Microsoft Visual Studio -> Visual Studio Tools -> Spy++.
WM_CHAR
WM_KEYDOWN
The helpful message above inspired me to create this snippet, which gives you a human-readable indication of what key was pressed from any WM_KEYDOWN/WM_KEYUP/WM_SYSKEYDOWN/WM_SYSKEYUP independent of the state of the modifier keys.
// get the keyboard state
BYTE keyState[256];
GetKeyboardState(keyState);
// clear all of the modifier keys so ToUnicode will ignore them
keyState[VK_CONTROL] = keyState[VK_SHIFT] = keyState[VK_MENU] = 0;
keyState[VK_LCONTROL] = keyState[VK_LSHIFT] = keyState[VK_LMENU] = 0;
keyState[VK_RCONTROL] = keyState[VK_RSHIFT] = keyState[VK_RMENU] = 0;
// convert the WM_KEYDOWN/WM_KEYUP/WM_SYSKEYDOWN/WM_SYSKEYUP to characters
UINT scanCode = (inLParam >> 16) & 0xFF;
int i = ToUnicode(inWParam, scanCode, keyState, outBuf, inOutBufLenCharacters, 0);
outBuf[i] = 0;
By modifying the keyState array so that all the modifier keys are clear, ToUnicode will always output the unshifted key you pressed. (So, on the English keyboard you'll never get '%' but always '5') as long as it's a human readable key. You still have to do the VK_XXX checking to sense the arrow and other non-human readable keys however.
(I was trying to rig up a user editable "hot key" system in my app, and the distinction between WM_KEYXXX and WM_CHAR was making me nuts. The code above solved that problem.)

Processing All Keyboard Inputs (Hooks)

I'm making a program that records all the keyboard actions, and stores this information into a log file (Keylogger). I just can't seem to find a good way of doing this.
What I have so far: A LowLevelKeyboardProc, The Virtual Key Code + the Scan Code of the Key being pressed.
What I would like: Using these codes, I will process and write information about the keyboard action being done. For invisible keys I would like the format: "[SHIFT], [ENTER], [ESC], etc. And for visible keys I would simply like their Ascii value (both Upper Case, and Lower Case), including if they enter: !##$%,etc..
I have a few ideas, but I don't know how I could capture everything. I have the information, I just don't know how to process it efficiently.
Refer to my post from here: Other Post
I've got example code for how to install a low-level keyboard hook and how to process the keystrokes.
Since you already have the hook working, all you need is a mapping from key codes to names for special keys. Just pre-populate an array of strings indexed by the key code:
const char *map[256];
map[VK_SHIFT] = "[SHIFT]";
map[VK_ENTER] = "[ENTER]";
...
Then in your hook function, check if the key is a printable character, if so, print it directly, otherwise lookup the name of the key and print that:
if (isprint(vkCode))
yourFile << char(vkCode);
else
yourFile << map[vkCode];