Converting keyboard input to char

Started by
6 comments, last by Juliean 13 years, 3 months ago
Hi,

Is there any command that converts a keyboard input like DIK_A into the equivalten letter, a in that case? I'm trying to make a name input for a highscore list but dont feel like writing 100 if-statements for all the possible characters.. Is there a way to do that easily? Output wouls be best in wchar format.
Advertisement
Do not use DirectInput for keyboard or mouse input (Microsoft themselves recommend against it). Instead, handle window messages - in your case, you'd be particularly interested in WM_CHAR.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

What API do you get DIK_A from? I googled but didn't come up with anything.

Typically for A-Z 0-9 I would think that the input keys would be the same as their ascii keycodes, so you could convert directly from one to the other.

Like on windows VK_A is the same as ascii 'A', VK_B is the same as ascii 'B' . If you want lowercase than it's just (VK_A + 0x20)
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
If you have the virtual key and scan code, for example from WM_KEYUP, you can use ToUnicodeEx to convert it into a wchar. It's not perfect, though. It have big trouble with dead keys, e.i. international letters that need more than one key press, but for simple input it's okay.
'benryves' said:
Do not use DirectInput for keyboard or mouse input (Microsoft themselves recommend against it). Instead, handle window messages - in your case, you'd be particularly interested in WM_CHAR.


Explanation whats against DirectInput? Do you mean in general (because window messages are slow as fark, aren't they?) for my whole game (yeah, that problem is for a game where I already use DirectInput) or just for that one problem?

'karwosts' said:
What API do you get DIK_A from? I googled but didn't come up with anything.


Its DirectInput. Should mean "Direct Input Key"..

[quote'karwosts']Typically for A-Z 0-9 I would think that the input keys would be the same as their ascii keycodes, so you could convert directly from one to the other.

Like on windows VK_A is the same as ascii 'A', VK_B is the same as ascii 'B' . If you want lowercase than it's just (VK_A + 0x20)

DIK_A equals to 0x1E. Is that correct for your example? And then, how do I get ascii to lpcwstr or std::wstring?

'Promethium' said:

If you have the virtual key and scan code, for example from WM_KEYUP, you can use ToUnicodeEx to convert it into a wchar. It's not perfect, though. It have big trouble with dead keys, e.i. international letters that need more than one key press, but for simple input it's okay.


Can you give a simple example for the use of ToUnicodeEx what every parameter is used when I for example want to get VK_A?

So to sum up there is no way to convert DirectInput to wchar isn't there?

Explanation whats against DirectInput? Do you mean in general (because window messages are slow as fark, aren't they?) for my whole game (yeah, that problem is for a game where I already use DirectInput) or just for that one problem?

Well, if you want to hear it directly from Microsoft
DirectInput is a set of API calls that abstracts input devices on the system. Internally, DirectInput creates a second thread to read WM_INPUT data, and using the DirectInput APIs will add more overhead than simply reading WM_INPUT directly. DirectInput is only useful for reading data from DirectInput joysticks; however, if you only need to support the Xbox 360 controller for Windows, then use XInput instead. Overall, using DirectInput offers no advantages when reading data from mouse or keyboard devices, and the use of DirectInput in these scenarios is discouraged.

'Promethium' said:

If you have the virtual key and scan code, for example from WM_KEYUP, you can use ToUnicodeEx to convert it into a wchar. It's not perfect, though. It have big trouble with dead keys, e.i. international letters that need more than one key press, but for simple input it's okay.


Can you give a simple example for the use of ToUnicodeEx what every parameter is used when I for example want to get VK_A?

Sure.

wchar_t buffer[5];
BYTE keyboard_state[256];
::GetKeyboardState( keyboard_state );
HKL keyboard_layout = ::GetKeyboardLayout( 0 );
int result = ::ToUnicodeEx( virtual_key, key, keyboard_state, buffer, 4, 0, keyboard_layout );
buffer[4] = L'\0';

if( result > 0 ) {
// Could convert, so do your stuff
}

Here 'virtual_key' is the wParam from WM_KEYUP and the 'key' is the scan code, e.i. bits 16-23 from the lParam: key = ( lParam >> 16 ) & 0xFF;
@Promethium:

Thanks! I sometimes have a hard time figuring out how to use all the parameters of a method but now I get it^^

@SiCrane:

Oh ok.. wasn't aware of that. Well, I wanna use joysticks&gamepads.. still stick to WindowsMessages or is DirectInput now a better option? I don't know what a DirectInput Joystick is oO
Apart from that I could re-write my input handling, wouldn't be much problem..

This topic is closed to new replies.

Advertisement