473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

Nov 15 '05 #1
2 10798
On 19 Aug 2005 08:46:15 -0700, gj*************@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
Nov 15 '05 #2

<gj*************@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.
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Bendzick | last post by:
Is there a simple way in python to read a keyboard scan code? I'm working on a shell script that interfaces with a proprietary keyboard device (extra buttons) and need to be able to distinguish...
2
by: Bill Zhao | last post by:
Dear all, Sorry for i did not paste original propreity codes See below the fake codes for what I cannot understand. =============================================== lass A { public: virtual...
13
by: Randell D. | last post by:
Folks, I have two related questions: 1. I have seen unicode being mentioned in my javascript pocket book - is this the same as ascii codes? I think not though I'm not sure and I can't find...
3
by: aman | last post by:
i know how to get scan codes and ascii codes for special keys. i can get ascii codes of alphabetic keys. how does one get scan codes of alphabetic keys??
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
7
by: Robert | last post by:
Has anyone had experience with using bar codes with C#. I have a client who wants to read a bar code with a patient's number on it and then look up the patient in a database. I know they will...
3
by: Mike | last post by:
Hi, Dim strTmp as String strTmp contains not only standart set of characters (ASCII codes from 1-127) but also some of them from the extended ASCII table (128-255). I need to output this...
18
by: Marcel Saucier | last post by:
Hello, I want to use the above characters codes chart but I dont know how to set the typeface (documentation: The characters that appear in Windows above 127 depend on the selected typeface). ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.