Virtual Key Codes, Scan Codes and ASCII Codes in C

G

gj_williams2000

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
 
J

Jack Klein

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 will be
able to answer this Windows API question.
 
M

Malcolm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top