capture key problem

This is a discussion on capture key problem within the C Programming forums, part of the General Programming Boards category; in c programming, how can I capture keys like "page down", "ctrl+?", etc. using standard library?...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    10

    capture key problem

    in c programming,
    how can I capture keys like "page down", "ctrl+?", etc. using standard library?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    You can't using the standard library. But you can use _kbhit() and _getch().

    gg

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    10

    I think that can't capture the 'page down'

    I think that can't capture the 'page down',

    any other method?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > I think that can't capture the 'page down',
    Think again then

    Extended keys arrive as two bytes - the first being zero
    Code:
    int mygetch ( void ) {
        int ch = getch();
        if ( ch == 0 ) ch = 256 + getch();
        return ch;
    }
    Now call this in a loop and print out the return value as you press each key, and study what is going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    If you are coding in DOS you write and interrupt 9 handler. If it's in Windows you simply check for the correct VK code - after masking out the correct bits. Check the Windows SDK on that.

    As for DOS (C++):


    Code:
    int keys[128];
    #define Keydown(scan_code)  (if keys[scan_code]?1:0)
    interrupt *OldHandler(...)=0;
    
    
    int main(void)
    {
       //Register atexit function
       atexit(UninstallHandler);
       ....
       ....
       return (0);
    }
    
    interrupt NewKeyHandler(...)
    {
      int x=inportb(0x60);
      if (x>127)
      {
          keys[x - 127]=0;
      } else keys[x]=1;
    }
    
    void InstallHandler(void)
    {
      getvect(0x09,OldHandler);
      setvect(0x09,NewKeyHandler);
    }
    
    void UninstallHandler(void)
    {
      setvect(0x09,OldHandler);
    }

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    thanks for all of you,

    to Salem,
    I know EXTENDED KEY needs two bytes,
    I also find a link full of these extended keys,as below:
    http://www.teletechnics.co.nz/refere...cii_codes.html

    but, How can I capture them?

    like:
    Code:
    char ch;
    ch=getch();
    
    if(ch=='????')
    {
       do sth.
    }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > I know EXTENDED KEY needs two bytes,
    But your example code says that you don't

    Read my code again - see how extended key codes get 256 added to them

    So from your handy table
    Code:
     		59		3B	073	00111011	F1
    You can write
    Code:
    int ch = mygetch( );
    if ( ch == 256 + 59 ) {
        // F1 pressed
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    get ctrl and key E or left eg.

    Code:
    #include <windows.h>
    
    // ... code
    
    if( GetAsyncKeyState(VK_CONTROL) &&
        GetAsyncKeyState('E') )
    {
        // do what ever
    }
    
    else if(GetAsyncKeyState(VK_LEFT) )
    {
        // do what ever
    }
    unfortuately if you want to check if it's capital letters inputted, u must check for the shift or caps button.

    all the virtual keys are here.
    http://msdn.microsoft.com/library/de...alKeyCodes.asp

    obviously this is only for windows

    somewhere in there is

    VK_PRIOR (21)
    PAGE UP key

    VK_NEXT (22)
    PAGE DOWN key

    also if ur using win NT/XP/2000 these work aswell

    VK_LSHIFT Left-shift key.
    VK_RSHIFT Right-shift key.
    VK_LCONTROL Left-control key.
    VK_RCONTROL Right-control key.
    VK_LMENU Left-menu key.
    VK_RMENU Right-menu key.
    Last edited by Kyro; 11-02-2003 at 01:54 AM.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    I am sorry to trouble you again, Salem

    Your method is working well for keys F1--F12,
    but the rest don't work.
    what's wrong?

    Code:
    int mygetch ( void ) {
        int ch = getch();
        if ( ch == 0 ) ch = 256 + getch();
        return ch;
    }
    
    main()
    {
       while(1)
       {
          if(mygetch()==256+81)   // 81 is PageDown
          break;
       }
    }

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Read the help on _getch(). Everything you needed to know is right there. Just have to read it.

    Code:
    int mygetch(void) 
    {
        int ch = getch();
    
        if (ch == 0) 
        {
            ch = 256 + getch();
        }
        else if (ch == 0xE0)
        {
            ch = 512 + getch();
        }
    
        return ch;
    }
    gg

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    The Problem has been solved

    thanks to all of you,
    Special thanks to Salem and CodePlug.

    to CodePlug and Salem,
    I think plus 512 and 256 is not needed in the code anymore.

    The Final Correct Code:
    Code:
    int mygetch(void) 
    {
        int ch = getch();
    
        if (ch == 0 || ch == 0xE0) 
        {
            ch = getch();
        }
        return ch;
    }
    
    
    main()
    {
       while(1)
       {
          if(mygetch()==81)   //81 is PageDown
          break;
       }
    }
    Last edited by goldenrock; 11-02-2003 at 07:54 PM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > if(mygetch()==81) //81 is PageDown
    And how will you tell the difference if the user presses 'Q' instead?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    Salem,
    thank you for giving me such an inspirational hint.
    I think I get it.
    You mean 256 is necessary, right?
    then the code must go this way:
    Code:
    int mygetch(void)
    {
       int ch=getch();
       if(ch==0||ch==0xE0)
       {
          ch=256+getch();
       }
       return ch;
    }
    
    main()
    {
       int ch=mygetch();
       
       switch(ch)
       {
       case 81:
          printf("you press 'Q' ");
          break;
       case 256+81:
          printf("you press 'page down' ");
          break;
       }
       getch();
    }
    -----------------------------------------------------------------

    by the way,
    I found that the arrow keys has no corresponding ASCII.
    but I wrote some codes to try it, and found that they also has two-character , and the first is -32.
    So I try to capture it the same way as above(simply replace 0 as -32).
    It works all right.
    Does it a correct way to capture arrow keys?
    -------------------
    how about the CTRL SHIFT and ALT ? I don't know how to try them.
    -------------------
    wish you can answer my boring questions altogether

    last,
    what's this below? another way of capture keys?
    #define SHIFT_R 0x0001
    #define SHIFT_L 0x0002
    #define CTRL 0x0004
    #define ALT 0x0008
    #define SCROLL_LOCK_ON 0x0010
    #define NUM_LOCK_ON 0x0020
    #define CAPS_LOCK_ON 0x0040


    thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  2. Mouvment problem
    By Death_Wraith in forum C++ Programming
    Replies: 9
    Last Post: 06-01-2004, 06:58 PM
  3. BCB Key press problem
    By Death_Wraith in forum Game Programming
    Replies: 0
    Last Post: 05-30-2004, 03:13 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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