Virtual Key Codes, Scan Codes and ASCII Codes in C

Discussion in 'C Programming' started by gj_williams2000@yahoo.co.uk, Aug 19, 2005.

  1. Guest

    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
    , Aug 19, 2005
    #1
    1. Advertising

  2. Jack Klein Guest

    On 19 Aug 2005 08:46:15 -0700, 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 20, 2005
    #2
    1. Advertising

  3. Malcolm Guest

    <> 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.
    Malcolm, Aug 20, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg  --
    Replies:
    4
    Views:
    2,138
  2. aman

    scan codes

    aman, Mar 1, 2004, in forum: C Programming
    Replies:
    3
    Views:
    2,342
    E. Robert Tisdale
    Mar 1, 2004
  3. Michael Bendzick

    Reading Keyboard Scan Codes

    Michael Bendzick, Jul 23, 2003, in forum: Python
    Replies:
    2
    Views:
    1,318
    Alan Gauld
    Jul 24, 2003
  4. softwarepearls_com
    Replies:
    10
    Views:
    4,707
    fylia
    Feb 26, 2009
  5. Pavel Pvl

    virtual key codes?

    Pavel Pvl, Dec 24, 2007, in forum: Ruby
    Replies:
    7
    Views:
    156
    Pavel Pvl
    Dec 30, 2007
Loading...

Share This Page