Thread: getch() - arrow key representation?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    getch() - arrow key representation?

    I'm making a small code, and I want my menu to be navigated by the arrow keys or (w, s, a, d). I'm hoping I can do both. I know there has to be an ASCII representation of an arrow key. Or is it interpreted as a different event then a keypress?

    I'm using windows XP dos console.



    To avoid double-posts I'll ask this here:
    Is there any way to center console text? (Other than trial & error spaces?)
    Last edited by Blackroot; 01-06-2006 at 04:09 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    No,arrow keys don't have an ASCII value.They are identified by their scan code.Scan code for various keys are
    Code:
    up-72
    down-80
    left-75
    right-77
    You may fetch these keys through following function
    Code:
    //Returns scan code of key that has been hit
    #include<dos.h>
    getkey()
    {
    union REGS i,o;
    while(!kbhit());
    i.h.ah=0;
    int86(22,&i,&o);
    return(o.h.ah);
    }
    Code:
    Is there any way to center console text? (Other than trial & error spaces?
    Yes,there are 25 rows and 80 columns in dos console.You can calculate it yourself how many tabs you may require to centre your text

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    So kbhit() can detect arrows?

    Also, my compiler is going nuts when I use your code, I'm no good with the (dos.h) library :/. (Borland c++ compiler.)

    My Code: (Noob to dos)
    Code:
    #include <dos.h>
    
    void getkey();
    
    int main() {
     return 0;
    }
    
    void getkey() {
     union REGS i,o;
     while(!kbhit());
     i.h.ah=0;
     int86(22,&i,&o);
     return(o.h.ah);
    }
    Errors:
    Code:
    Error E2450 C:\Burn.cpp 10: Undefined structure 'REGS' in function getkey()
    Error E2449 C:\Burn.cpp 10: Size of 'i' is unknown or zero in function getkey()
    Error E2450 C:\Burn.cpp 10: Undefined structure 'REGS' in function getkey()
    Error E2450 C:\Burn.cpp 10: Undefined structure 'REGS' in function getkey()
    Error E2449 C:\Burn.cpp 10: Size of 'o' is unknown or zero in function getkey()
    Error E2450 C:\Burn.cpp 10: Undefined structure 'REGS' in function getkey()
    Error E2268 C:\Burn.cpp 11: Call to undefined function 'kbhit' in function getke
    y()
    Error E2315 C:\Burn.cpp 12: 'h' is not a member of 'REGS', because the type is n
    ot yet defined in function getkey()
    Error E2268 C:\Burn.cpp 13: Call to undefined function 'int86' in function getke
    y()
    Error E2315 C:\Burn.cpp 14: 'h' is not a member of 'REGS', because the type is n
    ot yet defined in function getkey()
    Error E2467 C:\Burn.cpp 14: 'getkey()' cannot return a value in function getkey(
    )
    *** 11 errors in Compile ***
    Last edited by Blackroot; 01-06-2006 at 04:49 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I'm using windows XP dos console.
    You need to say which compiler, if you want to avoid all that old dos crud.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    (Borland c++ free command line compiler.)
    /\
    Thats my compiler. I'm fairly sure its dos library is up-to-date, dont take my word on that though, not totaly sure.
    Last edited by Blackroot; 01-06-2006 at 05:41 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    So maybe you should be reading Adrian's win32 console tutorial which is getting a lot of air time at the moment.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah tyvm! You guys are great XD.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM