Jump to content
  • Advertisement
Sign in to follow this  
Juliean

Converting keyboard input to char

This topic is 2805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote='benryves']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.[/quote]

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?

[quote='karwosts']What API do you get DIK_A from? I googled but didn't come up with anything. [/quote]

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)[/quote]

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

[quote='Promethium']
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.[/quote]

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?

Share this post


Link to post
Share on other sites

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.[/quote]

Share this post


Link to post
Share on other sites

[quote name='Promethium']
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?[/quote]
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;

Share this post


Link to post
Share on other sites
@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..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!