Scan->Charcode again

Started by
4 comments, last by Dtag 21 years ago
Hi last time i posted someone showed me the article on this page that proposed the following function to convert scan to ascii codes: static int scan2ascii(DWORD scancode, ushort* result) { static HKL layout=GetKeyboardLayout(0); static uchar State[256]; if (GetKeyboardState(State)==FALSE) return 0; UINT vk=MapVirtualKeyEx(scancode,1,layout); return ToAsciiEx(vk,scancode,State,result,0,layout); } I created a test app where it worked fine. But in my Engine ( that uses OpenGL for drawing and DInput for input ) the code doesnt recognize that shift is pressed for example. The keyboardstate is acquired properly ( i checked it with step by step debugging ) So if i hold shift and then press h, the output is ''h'' instead of ''H''. Does anyone have an idea what could be wrong? Thanks alot
Advertisement
If I understand you correctly you want to convert the Virtual Key Code for A (0x40) to the ascii character ''a'' (0x61). But you also say you are using Direct Input which does not use virtual keys and will return DIK_A (0x1E) for ''a''.

I have yet to find a better way to convert DIK_A to ''a'' than to create an array of characters with the index of the Direct Input code.
I want to convert the Scancodes generated by DI to their Ascii representative considering the current Shift State etc. So if i hold shift and call that Scan2Ascii function with DIK_H as parameter for example, i want it to output ''H'' .. in my test program it does that without any problem... but in my engine it outputs ''h''
Try creating an array of Characters with the index equal to the DIKEY.
char DI2Char[] = { ' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', ' ', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', ... }   

If IsAlpha(DI2Char[scancode]) AND Shift Key is down Then
return DI2Char[scancode] + 32
Else
return DI2Char[scancode]
End If

Hope This Helps


[edited by - Enfekted on April 20, 2003 3:44:03 PM]
This is not the proper way to do it and it wont even work for ppl in other countries. The solution must have to do with the function i posted i guess
I just debugged a little more... This is my function im using to get the keyboard input:

void CEngine:ollKeyboard(){	DIDEVICEOBJECTDATA didod[16];  // Receives buffered data 			if (!m_pKeyboard) 		return;	DWORD dwElements = 16;	HRESULT hr = m_pKeyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),		didod, &dwElements, 0 );			if (hr==DIERR_INPUTLOST )		SetAcquire();// P1//	WORD ret[2]={0}; //	Scan2Ascii(DIK_H,(WORD*)&ret);		if (FAILED(hr))	{		return;	}// P2//	WORD ret[2]={0}; //	Scan2Ascii(DIK_H,(WORD*)&ret);// Below here is some more code but that doesnt really matter to the problem  


You can see the 2 comments P1 and P2 up in the code. If i test the Scan2Ascii function at P1, everything works perfectly, it can distinguish capital from normal letters and detects when shift is down etc.
At P2 it only returns normal non capital letters even while shift is down. Any ideas? Im completely clueless (

[edited by - Dtag on April 21, 2003 6:29:53 AM]

This topic is closed to new replies.

Advertisement