Specific Keys in C, getting and putting

This is a discussion on Specific Keys in C, getting and putting within the C Programming forums, part of the General Programming Boards category; Hey everyone how yall doin', i've got a programming problem in C, probably easily cured, but I can't say for ...

  1. #1
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    Specific Keys in C, getting and putting

    Hey everyone how yall doin', i've got a programming problem in C, probably easily cured, but I can't say for sure.

    Alright how do you flag down individual keys on a keyboard, getchar() just don't cut it, for 2 reasons:
    1. I don't like the post enter delimma (Yes i've read the Eskimo guy's note)
    2. It's only for ASCII and that doesn't cover ALL keys

    So really my question is, what function can I use to get the SPECIFIC character that a user presses

    Code:
    ex.
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        printf("Press a key: ");
        int key=getchar();
        printf("You pressed %d\n",key);
    
        system("PAUSE");
        return 0;
    }
    I don't care if it returns a number or character, I just want a function, and probably not getchar(), to return the specific value of whatever key was pressed, and even diffrenciate between regular 1 and num pad 1, as well as include all other "standard" 102 key keyboard keys like "del", "sys rq" or "arrow up".

    Thanks so much for taking the time to read this and for the time of your reply, it really means alot.
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You simply don't understand how key presses work. Some keys (the arrow keys, the F keys) are counted as more than one keypress. Not just a single keystroke. Basicly what you do is read a keystroke, test the first value. If it's the escaped sequence, then you read the second value to see what escaped key they've pressed.

    For example, and 'a' is just an 'a'. Others are a pair of keys. The first is a zero, not a '0', but the actual value zero, followed by another keystroke, which is then translated to the other keystroke.

    Anyway, there is no ANSI standard way to read a keystroke. It will depend on your OS and compiler.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    also don't declare variables in the middle of your program. That is c99 standard and c++ not c

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    You guys and your standards. Give the guy a break.



    Trapping keys via the built-in C functions are going to be both slow and cumbersome. In Windows there are several functions that test for keys - all of em. In DirectX, you are given full control of the keyboard....just enough to make you dangerous anyways.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Bubba
    You guys and your standards. Give the guy a break.
    Why? They didn't specify anything about the envoirnment they were writing for. Thus, it's only logical to provide them the best answer possible for the given parameters of the question. See below.
    Originally posted by Bubba
    Trapping keys via the built-in C functions are going to be both slow and cumbersome. In Windows there are several functions that test for keys - all of em. In DirectX, you are given full control of the keyboard....just enough to make you dangerous anyways.
    Your answers are absolutely worthless if he isn't working with Windows. That's the reason for mentioning standards.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    also don't declare variables in the middle of your program. That is c99 standard and c++ not c
    Hmm...unless I can't read I can't see anything in this about platform or OS. That was not the reason for him posting about standards and such.

  7. #7
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    Question Update

    Sorry about not knowing the specifics on keyboard input, I literally have no clue about that (although not because of lack of effort). So I guess I should just ask is there a site any of you all know that tells about how keyboard input works or anything (because i've looked up and down on google and this site and there isn't anything) like that. All I really want is the knowledge of how to tell what key or keys a user is pressing, like Ctrl+S for save or again how to tell what combination of key codes makes up a left shift or a right shift.

    Thanks alot for all the help thus far, I really appriciate it.
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    didn't mean to start a war. Let's get back to the question. Is there a standard way? I don't know how to get two keys from a user at the same time. I think each operating system would have its own 2byte integer for that keypress. I am just guessing I have no clue. Does anyone here know?
    Last edited by linuxdude; 01-05-2004 at 10:55 PM.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    If you don't want to getch(), as this thread shows, then you'll have to use GetStdHandle(), SetConsoleMode(), and ReadConsoleInput() - all in windows.h.

    Assuming you're own some flavor of Windows, unless you have a PAUSE script.

    gg
    Last edited by Codeplug; 01-05-2004 at 11:06 PM.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: Update

    Originally posted by Axpen
    All I really want is the knowledge of how to tell what key or keys a user is pressing, like Ctrl+S for save or again how to tell what combination of key codes makes up a left shift or a right shift.

    Thanks alot for all the help thus far, I really appriciate it.

    6 of the 8 posts that responded to you mentioned in some way that your request is:
    1) compiler specific -- this means we cannot give you an answer because you have yet to tell us what compiler is being used
    2) system specific -- this means we cannot give you an answer because you have yet to tell us what operating system is being used

    Did you somehow miss that? Even from all these clues:
    It will depend on your OS and compiler.
    In Windows...
    They didn't specify anything about the envoirnment they were writing for...
    I can't see anything in this about platform or OS.
    I think each operating system would have its own ...
    Assuming you're own some flavor of Windows
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    What i'm using

    Alright i'm using Dev-C++ 4.9.8.0 (Just downloaded two nights ago) and am using Windows XP home.

    I may have found a solution but Dev-C++ doesn't have much documentation on the topic, the solution is using inline assembly and using DOS interrupt 16h to catch the scan code as well as the ASCII code but I can't figure out how to get inline assembly to work in Dev-C++.

    So far this is what I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getKey(void);
    
    int main(int argc,char *argv[])
    {
      int x;
      puts("Press a key\n");
      x=getKey();
    
      printf("You pressed: \"%04X\"",x);
    
      system("PAUSE");
      return 0;
    }
    
    int getKey()
    {
      int key;
    
      __asm mov ah,10h
      __asm int 16h
      __asm mov key,ax
    
      return(key);
    }
    That was basically the same as the code from the author of C for Dummies Code and Concept Here but that nor
    Code:
    __asm
    {
      mov ah,10h
      int 16h
      mov key,ax
    }
    that works, any ideas on Dev-C++ inline assembly would be GREATLY appriciated, thanks.
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  12. #12
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by linuxdude
    also don't declare variables in the middle of your program. That is c99 standard and c++ not c

    To be honest, i find its sometimes more productive and reader friendly. There is no way you know what you will need all the time before you code.

    Besides if C++ was meant to be a more porductive way to code C, then that means it is good form, and practice. IMHO.

    In anycase. Its more an asthethic style.


    I'm also glad to see your not copying the authours code verbatim. As you can see, even main doesn't return an integer. I've been warnded to stay away from books like that.
    Last edited by caroundw5h; 01-06-2004 at 07:38 AM.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    >> Alright i'm using Dev-C++ 4.9.8.0 (Just downloaded two nights ago) and am using Windows XP home.
    If you want to write a Windows application, then use the links in my previous post.
    If you want to write a DOS application that runs in the Virtual DOS Machine (VDM) in WindowsXP, then you need to download another compiler: DJGPP.

    The code you're trying to use will only work in DOS.

    gg

  14. #14
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Originally posted by caroundw5h
    To be honest, i find its sometimes more productive and reader friendly. There is no way you know what you will need all the time before you code.

    Besides if C++ was meant to be a more porductive way to code C, then that means it is good form, and practice. IMHO.

    In anycase. Its more an asthethic style.
    Well we program in C, so why practice c++ when writing programs in C?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  15. #15
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    Question Problem solved, but with confusion

    I got and working thanks to the link to here, however, although it solved my problem there was something there I didn't understand, the 0xE0, what significance does that play? I understand 256's role, since extended characters that aren't ASCII have null ASCII code you need more than the first byte of scan code. Even after reading msdn's reference and goldenrock's ASCII key code link I still don't understand the 0xE0 (and yes i'm aware that's 224 in decimal) I still don't understand it.

    Thanks so much everyone,
    Alex
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21