Thread: getch();

  1. #1

    getch();

    What will getch() return for these keys:

    Left Arrow
    Right Arrow
    Up Arrow
    Down Arrow
    ESC
    F1-12
    Tab
    Enter
    Shift
    CTRL
    ALT
    Backspace

    like in this

    switch (getch())
    {
    case LEFT ARROW:
    blah blah
    break;
    }

  2. #2
    Disagreeably Disagreeable
    Join Date
    Aug 2001
    Posts
    711
    Why don't you enter this in the compiler and check it for yourself:

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
       char ch = getch();
    
       cout << ch << endl;
       return 0;
    }
    Most if not all of those keys will cause the program to not display anything. This is because they do not have corresponding ASCII codes.

    Of course, ya never know. A few might bring some unexpected results...

  3. #3
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859

    Red face

    Up, down, left, and right arrow shows 0 in my ASCII codes...
    Yoshi

  4. #4
    Disagreeably Disagreeable
    Join Date
    Aug 2001
    Posts
    711
    Well, there ya have it.

  5. #5
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    up is 72,
    down is 80,
    right is 77,
    left is 75

    If this is what you're looking for, no thanx needed. if not, Then i don't know
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  6. #6
    Okay, I got it to do letters. K = left H = up P = down M = right

    Now when I press an arrow it returns zero the first time I press it, then it returns the letter. WTF? Why does it do this, and can I override it doing that? Or do I need to just tell the user to press left twice or down twice to move.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I think it has something to do with upper and lower bytes... Well, I don't know so I should probably just shut up .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Unregistered
    Guest
    hi

    simply use this code:

    x=getch();
    if(x==0) // is special key else is normal key (a, b, RETURN, ESC ....)
    y=getch(); // number of y is the key pressed (up, down, F1, F2...)

    i hope this helps.

    bye


    PLEASE HELP ME WITH MY MOUSE IN 800x600 24 BIT MODE!!!

  9. #9
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    You can try this out if you like. A sort of in the works project.

    #include <stdio.h>
    #include <conio.h>

    #define KEYBOARD 0x60
    #define KCONTROL 0x61
    #define KINTCONTROL 0x20

    #define LARROW 75
    #define RARROW 77

    // continue from there

    #define F_1 59
    #define F_2 60

    // continue from there

    int key;

    int getKey(void);

    int main()
    {

    getKey();

    switch(key)
    {
    case LARROW: printf("\nleft arrow key pressed.");
    break;
    case F_1: printf("\nF1 key pressed.");
    break;
    // and so on

    }

    return 0;
    } // end main

    int getKey(void)
    {
    printf("\nPress a key: ");
    key = getch();

    _asm
    {
    sti
    in al,KEYBOARD
    xor ah,ah
    mov key,ax
    in al,KCONTROL
    or al,82h
    out KCONTROL,al
    and al,7fh
    out KCONTROL,al
    mov al,20h
    out KINTCONTROL,al
    }
    return key;
    }

    I hope there are no typoes as this was rushed.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  10. #10
    thats okay, I'm just going to make it loop the getch() again if it's zero. That way it don't flash, because the character and the HP/MP flash everytime you take a step for some reason.

  11. #11
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Ah, A RPG hound then? A 3D game?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  12. #12
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    for any one who cares.

    did this, it gets the arrow keys and does *something* depending on which arrow key. Pretty simple, should be easy to modify for your use.

    //code:
    void cursor()
    {
    // getch();

    int c = 1;
    int ch = 0;

    while (ch != c)
    {
    ch = getch();

    // cout << ch << endl;
    if(ch == 72) // if UP
    cout << "^" << endl;
    if(ch == 80) // if Down
    cout << "d" << endl;
    if(ch == 75) // if left
    cout << "<" << endl;
    if(ch == 77) // if right;
    cout << ">" << endl;
    }

    }
    // ^^ end code!
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM