At the moment i use a system such as:
case WM_KEYDOWN:
keys[wParam] = true;
Which doesnt work for lowercased letters or special characters such as "&", so im asking is there a winapi function to read keyboard so i could get any 8bit character from user, if he writes "Æ" i would get the corresponding index for that char in this table:
(in case image not working: http://img801.imageshack.us/img801/1965/asciipage.png )
Im using this table to render text in my OpenGL application, so i need to find from this table which character the user keyboard generated to my program chat line, so im trying to make a chat in my game.
There is the WM_CHAR message which gives you the fully translated character code. Your message loop must use TranslateMessage prior to DispatchMessage for propper keycode to character translation.
Related
Ok, so I'm trying to develop an app using C++ and Qt4 for Linux that will map certain key sequences to special Unicode characters. Also, I'm trying to make it bilingual, so the special Unicode character sent depends on the selected language. Example: AltGr+s will send ß or ș, depending whether German or Romanian is selected. On Windows, I have achieved this using AutoHotKey. However, I couldn't get IronAHK to work on Linux so I have written myself a nice Qt Application for it, using Qxt to register "global" shortcuts. I have tried this snippet:
void mainWnd::sendKeypress( unsigned int keycode )
{
Display *display = QX11Info::display();
Window curr_focus;
int revert_to;
XGetInputFocus( display, &curr_focus, &revert_to );
XTestFakeKeyEvent( display, keycode, true, 0 );
XTestFakeKeyEvent( display, keycode, false, 1 );
XFlush( display );
}
copied from another application(where it works), but here it seems to do nothing. Also, there might be a problem with the fact that the characters I'm trying to send aren't found on a US 101 Keyboard, that I currently use on my laptop(and as the layout in the OS).
So my question is: how do I make the app send a Unicode character to whichever app has focus, inserting a special character(sort of like KCharMap)? Remember, these are special characters which are not found on a normal US Keyboard. Thanks in advance.
Good day. There was a need to monitor typed text, ie No keystrokes, but namely derived characters.
At the beginning I use the raw input method for obtain a virtual key codes of pressed keys, but this decision had to be abandoned because failed to properly convert virtual key codes to characters.
MapVirtualKeyEx + LoadKeyboardLayout - did not give the desired effect, ie if the load the Russian layout by calling LoadKeyboardLayout, then MapVirtualKeyEx still returns to Latin A for code 0x41.
So I began to use the hook WM_GETMESSAGE.
if (nCode < 0)
return CallNextHookEx(NULL, nCode, wParam, lParam);
// WH_GETMESSAGE
if (nCode == HC_ACTION)
{
PMSG pMsg = (PMSG)lParam;
if (pMsg != NULL)
{
if ((pMsg->message == WM_CHAR)||(pMsg->message == WM_UNICHAR))
{
keyEntry.character = pMsg->wParam;
ipc->SendData(&keyEntry, sizeof(CHARENTRY));
};
};
};
return CallNextHookEx(NULL, nCode, wParam, lParam);
Everything works fine, including for the characters with Dead key.
But when I tried to type the text in the editor of a visual studio, problems occured:
For one key press comes two identical messages WM_CHAR;
For the Cyrillic characters the value WM_CHAR-> wParam does not match the characters typed, for Latin - all right.
How to convert virtual key code to a character considering input language of the process in which the text is typed + able to recruit composite characters (for example: ô). Or tell me how to solve the problems in the version with hooks.
Thanks in advance )
A keyboard generates two scan codes when the user types a key — one when the user presses the key and another when the user releases the key.
Source : About Keyboard Input - http://msdn.microsoft.com/en-us/library/windows/desktop/ms646267(v=vs.85).aspx
WM_CHAR will only be UTF-16 if the application is compiled for Unicode as
the character encoding (i.e. the difference between MBCS and UNICODE). If
your application is built with MBCS (multi-byte character set), then the
system will use the current system code page to convert the Unicode
character to "Ansi" (in quotes) and send that.
So check in your build settings for which character encoding your app is
configured for.
Source : Unicode and Multibyte Character Set (MBCS) Support : http://msdn.microsoft.com/en-us/library/ey142t48(v=vs.80).aspx
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.)
So i'm creating my own edit control (multi-line textbox) in C++ using the windows API. It's going pretty well, but i'm a bit confused about one thing.
First off, the control is being built to be able to handle unicode and all input will be converted to unicode. In other words, all input will be stored as wchar_t.
What i'm confused about is which message to process for keyboard input. MSDN has the following window notifications:
WM_CHAR
WM_KEYDOWN
WM_UNICHAR
And others, but i believe it's one of these three that i need to process. My guess would be WM_UNICHAR, but the documentation is a bit unclear about it. Also, upon looking over the VKcodes, i saw this:
VK_PACKET
0xE7
Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP.
Sorry if it's a silly question, but i just want to be sure about this.
If your control is created as a unicode window (using CreateWindowW) then
in WM_CHAR you will get wide char out of the box.
If you want to provide non-unicode version of your control then you need to handle
WM_INPUTLANGCHANGE, something like this:
case WM_INPUTLANGCHANGE:
{
HKL NewInputLocale = (HKL) lParam ;
g_InputCodePage = LangToCodePage( LOWORD(NewInputLocale) ) ;
}
And so your WM_CHAR handler should look like:
case WM_CHAR:
{
unsigned char c = (byte)wParam;
if(!::IsWindowUnicode(hwnd))
MultiByteToWideChar(g_InputCodePage , 0, (LPCSTR) &c, 1, (LPWSTR) &wParam, 1) ;
}
And don't forget about WM_IME_CHAR and friends.
And yet about RTL input.
WM_KEYDOWN is sent to the window with the focus when a non-system key has been pressed. When the message is translated by the TranslateMessage function, a WM_CHAR message is sent to the window. WM_CHAR uses UTF-16. WM_UNICHAR is similat to WM_CHAR, except that it uses UTF-32. It's purpose is to send/post Unicode characters to ANSI windows. If the window is ANSI (created with CreateWindowA), when WM_CHAR is generated. If it is Unicode (created with CreateWindowW) then WM_UNICHAR is generated. So your control should probably handle both.
Also see this discussion Why is my WM_UNICHAR handler never called?.
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];