![]() |
Virtual Key Codes, Scan Codes and ASCII Codes in C
Not sure if this is the right board but it is in c...
I've got a console program written in c which receives key presses and gets a Windows Virtual key code. I am trying to convert it to the exact ascii value the user has typed. so if shift is down it should be a capital otherwise lower case etc. I started with this: void printKey(int key) { printf("Key: %c\n", key); } and I got all upper case (not really suprising) so then after a bit of research and grafting I came up with this: int vk2ascii(unsigned int vk, int *s) { int scan; unsigned char state[256]; HKL layout=GetKeyboardLayout(0); if(!GetKeyboardState(&state)) return 0; scan=MapVirtualKeyEx(vk, 0, layout); return (ToAsciiEx(vk, scan, state, s, 0, layout)>0); } s should now contain the correct ascii value and so if passed to the printKey function I thought I would get the right character but instead everything just seems to be lowercase! Does anyone have any ideas or can anyone point out where I'm being dumb!! Thanks for readin', Gareth Williams |
Re: Virtual Key Codes, Scan Codes and ASCII Codes in C
On 19 Aug 2005 08:46:15 -0700, gj_williams2000@yahoo.co.uk wrote in
comp.lang.c: > Not sure if this is the right board but it is in c... Well it is not standard C, but C with extensions, and unfortunately platform specific extensions are off topic here. > I've got a console program written in c which receives key presses and Here's where you cross the line. There are no such things as keys in C, and certainly no requirement that keys exist. Standard C provides input and output only in terms of FILE* streams. > gets a Windows Virtual key code. I am trying to convert it to the exact > ascii value the user has typed. so if shift is down it should be a > capital otherwise lower case etc. The people over in news:comp.os.ms-windows.programmer.win32 will be able to answer this Windows API question. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html |
Re: Virtual Key Codes, Scan Codes and ASCII Codes in C
<gj_williams2000@yahoo.co.uk> wrote > > int vk2ascii(unsigned int vk, int *s) > { > int scan; > unsigned char state[256]; > HKL layout=GetKeyboardLayout(0); > > if(!GetKeyboardState(&state)) > return 0; > > scan=MapVirtualKeyEx(vk, 0, layout); > return (ToAsciiEx(vk, scan, state, s, 0, layout)>0); > } > > s should now contain the correct ascii value and so if passed to the > printKey function I thought I would get the right character but instead > everything just seems to be lowercase! > > Does anyone have any ideas or can anyone point out where I'm being > dumb!! > You've got the right idea. To make the portion of your program which is ANSI C interact with system-specific parts, like the Microsoft keyboard driver, you need to write functiions to convert between the Microsoft codes and the C internal representation (almost always ASCII, but your program shouldn't care). You must be doing something wrong with checking the shift key and the call to the Microsoft functions. I'm not sufficiently familar with them to spot exactly what is wrong, but to solve this type of problem 1) look at Microsoft's documentation and see how they handle the shift key 2) do a little exploratory programming to see if the functions behave as you think they do, and try to genrate both upper and lower case characters. 3) If what the documentation says and the way the program behaves doesn't seem to match, look at the documentation again and try to see if you have misunderstood. If you are absolutely sure that the functions are not behaving as Microsoft says they should, file a bug report and try to work round. |
| All times are GMT. The time now is 11:51 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.