|
Hey everyone,
I'm writing an application in which I need to convert an input of virtual key codes to an ASCII output. Currently I have something like this:
if (logchar>0x40 && logchar<0x5a){
if ((shiftKeyDown==FALSE && capslockKeyDown==FALSE)
|| (shiftKeyDown==TRUE && capslockKeyDown==TRUE)) logchar+=0x20;
}
else if (logchar>=0x30 && logchar<0x40 && shiftKeyDown==TRUE){
switch(logchar)
{
case 0x31:
logchar='!';
break;
logchar contains the virtual key code.
So far, I've successfully implemented the alphabet. Unfortunately, I can't convert other symbols like ,./<>?;':"[]{} etc. I have absolutely no idea what logchar would equal if the input is a key like ~ or |. If anyone could give me a list of the virtual key codes for each key on the keyboard that would be appreciated.
And if there is any other function that would make this easier that would also be appreciated. I'm doing everything with switch and if/else statements and it's getting quite messy.
Thanks in advance.
|
|
|
|
|
You cannot use MapVirtualKey(...) along with the state of the modifier keys (like <SHIFT>)?
Also, a list of Virtual Key codes is available in the MSDN documentation.
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Thanks! This is almost what I'm looking for. I haven't tried implementing it yet, but from the documentation on MSDN it looks like it'll do the job I want it to do.
I'm pretty sure I can detect the modifier keys and change the output with an if/else statement.
I'll try that and see how it goes.
Thanks again.
Edit: Ok. It works now. The only problem that I have no idea how to fix is how to convert symbols. Like, for example, if the input is a vkcode for [ or ] or \, the output comes out as 0. Does the MapVirtualKey function support symbols? Or is it just letters?
modified on Tuesday, September 23, 2008 12:43 AM
|
|
|
|
|
Why don't you handle the WM_CHAR messages instead ?
What kind of application are you doing (MFC, win32, ...) ?
|
|
|
|
|
I'm writing an MFC application.
I can't handle WM_CHAR because I'm getting the vkcodes from a LL_keyboard hook.
|
|
|
|
|
You might want to check out ToAsciiEx()[^] too.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Just one question about that function: what's a hardware scan code? MSDN says
uScanCode<br />
[in] Specifies the hardware scan code of the key to be translated. The high-order bit of this value is set if the key is up (not pressed).
Also, for lpKeyState, MSDN says
lpKeyState<br />
[in] Pointer to a 256-byte array that contains the current keyboard state. Each element (byte) in the array contains the state of one key. If the high-order bit of a byte is set, the key is down (pressed).<br />
<br />
The low bit, if set, indicates that the key is toggled on. In this function, only the toggle bit of the CAPS LOCK key is relevant. The toggle state of the NUM LOCK and SCOLL LOCK keys is ignored. How would I obtain that? I'm getting input from an LL_keyboard hook, so is there any function I could use?
Finally, I'd like to ask whether this function can map symbols like ,./<>?;':"[]\{}| as well. The MapVirtualKey function can't do that as far as I'm concerned.
Thanks for your response
|
|
|
|
|
Yes, ToAscii can map symbols like those too. What's special with those characters? After all, they are the outcome of a keypress and I am sure that must work.
I wanted to write a sample application for you, but I was (and am) just too busy. I'll write to you in a day or two. Ping me back if I forget and if you were not able to do it.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
THANK YOU SO MUCH
My app is running flawlessly now. I've tested every single key on the keyboard and it works fine. (well, except for the non-character keys).
I only have one question left. What happens in my program when I press one of the non-character keys? What will the output be? Here is my code:
static unsigned char keystate[256];
if (GetKeyboardState(keystate) == FALSE) return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
ToAscii(logchar,NULL,keystate,(unsigned short *)&logchar,0);
I'm just wondering how it converts a non-character key to ASCII
Thanks again for your help. I appreciate it very much.
|
|
|
|
|
hxhl95 wrote: What happens in my program when I press one of the non-character keys?
The hook will trap every key for sure and you can handle it all. Non-Character? For example, like which key?
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Well, lets say I press the caps lock key. what would the output be?
i viewed the output in notepad and it's something like 0x2B. is that the ascii representation of the caps lock key?
also, for the ToAscii function (i'm using toascii instead of toasciiex), is it ok if i pass NULL to the uScanCode parameter? i'm doing that right now and the output seems to be fine.
thanks again for your help.
|
|
|
|
|
Virtual key code for caps lock key is VK_CAPITAL (0x14) - more here, just in case you need it: Virtual-Key codes[^].
You might want to know the status of shift key every time (you may use GetKeyState()[^]) for this, to check while the user is pressing keys like '['. If the shift was pressed, the actual character would be '{'. This applies if you are interested in the casing (upper/lower) of the characters typed.
hxhl95 wrote: is it ok if i pass NULL to the uScanCode parameter
Yes, that shouldn't be a problem. I was going to write a small app for you, but then I'm a little unwell and taking some rest for 2 days. If I go for work tomorrow, then I'll do this for you. That will probably clear off all your doubts.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
You've got this done? You want me to write a small app? I'm back at work.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Yeah, it's working fine now.
Thanks for all your help.
|
|
|
|
|
Hello Rajesh,
I am facing problem while convert Virtual key code to ascii.
Here is my code
<br />
char ch; <br />
WORD w = 0;<br />
BYTE ks[256];<br />
GetKeyboardState(ks);<br />
BYTE nKeyState = GetKeyState(wParam);<br />
UINT uScanCode = MapVirtualKey(wParam,MAPVK_VK_TO_VSC);<br />
int res = ToAscii(wParam,uScanCode,&nKeyState,&w,0);<br />
ch = (char)w;<br />
Everytime w returns 63 i.e question mark for all key..
Can you please help me out where i was wrong..
Thanks in advance
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
Hey All,
Hope this is the right group.
I'm trying to find out the behaviour of WaitForSingleObject when the passed in handle is undefined (NULL or freed by another thread). I haven't seen any article that explains the behaviour of the call in this case. The reason I'm asking is I'm trying to track down a random crash in a multi-threaded application that makes it appear that my mutex is being ignored.
Thanks!
Anthony
|
|
|
|
|
anthonym7 wrote: freed by another thread).
Do you mean closed? You don't free handles (in the sense of malloc / free ). If so, MSDN is very clear:
"If this handle is closed while the wait is still pending, the function's behavior is undefined."
|
|
|
|
|
"If this handle is closed while the wait is still pending,
the function's behavior is undefined."
If the handle is NULL, WaitForSingleObject returns WAIT_FAILED and
GetLastError() returns ERROR_INVALID_HANDLE.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I believe that if you call WFSO with an invalid handle, it will return WAIT_FAILED and GetLastError() will return ERROR_INVALID_HANDLE . Have you tested this to see? Try passing in NULL /0 or a arbitrary value like 0xDEADBEEF .
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
As you already have the answers (which are available in the documentation, but we won't mention that ) I will point out that in my experience having to test for closed or invalid handles can be indicative of bad design. Adding conditional code that executes when the error is discovered will most likely result in yet more random unexpected behavior.
led mike
|
|
|
|
|
Thanks for your thoughts... but I guess I deserve some of the snarky comments. I'm trying to boil down a very complex problem into a few sentences and past experience shows that usually doesn't work. Perhaps I should have proofread my question a bit more carefully.
I never suggested anything in my original postings, just wanted your thoughts on what might happen with WFSO since we are seeing in rare situations a case where the mutex is failing. The system is multi-client/multi-threaded. The handles look fine but WFSO is not behaving as expected.
When we find a resolution, and the answer isn't too complex, I'll reply here.
Thanks again.
|
|
|
|
|
I Guess
Very Complex Problems need to be boiled down to a number of simple problema. I've No Idea of your experience, but, as you are using WFSO, you must have at least two threads in your code. Set Watch and Breakpoints. Set Time aside to Design, and Develop in Small Steps, adding a thread at a time. In my experience, WFSO has never misbehaved! (Not even in WIN95). What happens if you pass it a Crapp handle? Don't know, try it, I doubt that it will crash! The returned value may even be different between (versions of)OS. That's where 'default' in the switch to deal with possible results comes in handy.
Regards
Bram van Kampen
|
|
|
|
|
Hi
I am trying to split the header of a list control into multiple sub-header, but not finding a way to do so…
To put in other words…
I have added say 3 columns named col_1, col_2, col_3 in list control. Now I want to split col_2 into say col_2_sub_1 and col_2_sub_2. Whats the way to do so?
Any help would be appreciated.
Regards
Sameer Thakur
|
|
|
|
|
It sounds like you want to make another column but I don't think anyone clearly understands your question.
led mike
|
|
|
|
|
I stand awaiting correction. This is my interpretation of the question.
------------------------- -------------------------
|_______|_______|_______| |_______|_______|_______|
| | | | ------> | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
I hope I'm wrong - achieving that is going to make getting these pics look right seem like fun
|
|
|
|