How to ignore arrow key pressing

This is a discussion on How to ignore arrow key pressing within the C++ Programming forums, part of the General Programming Boards category; Code: while(choice != 'f' && choice != 'm') { if(kbhit()) { choice = getch(); if(choice == 0) { choice = ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    How to ignore arrow key pressing

    Code:
                             while(choice != 'f' && choice != 'm')
                             {
                                 if(kbhit())
                                 {
                                          choice = getch();
                                          if(choice == 0)
                                          {
                                          	choice = getch();
                                          }
                                 }
                                 if(isalpha(choice))
                                 {
                                    choice = tolower(choice);
                                 }
                             }
    When i press an arrow key i get 1 of 4 values (M,H,L,P)
    How do i avoid this. Trying to block the value of the extended arrowkey causes me to block also the Capital letter. such as
    Code:
    if(c == 0x04b)
    Who needs a signature?

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Hello? A little help?
    Who needs a signature?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, I'm not sure what exactly you want to know. You are checking whether it was an arrow/special key, with your test ==0. If you don't want the key code in that case, just do getch() instead of choice = getch().

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    I want the user to either press Capital or small F/M but they can press right arrow and it outputs an m i want to know how to avoid this.
    Who needs a signature?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Nathan the noob View Post
    I want the user to either press Capital or small F/M but they can press right arrow and it outputs an m i want to know how to avoid this.
    So if it's a special key don't keep the keypress.
    Code:
                             while(choice != 'f' && choice != 'm')
                             {
                                 if(kbhit())
                                 {
                                          choice = getch();
                                          if(choice == 0)
                                          {
                                            /*You keep saying you don't want this key SO DON'T READ IT IN
                                          	choice = getch(); */
                                            getch(); /*to discard the special key*/
                                          }
                                 }
                                 if(isalpha(choice))
                                 {
                                    choice = tolower(choice);
                                 }
                             }

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    If i remove it from the eqaution and press Any of the 4 arrows or home end page up/down or delete i get a letter of the alphabet. Im on windows xp.

    Like Left arrow = K.
    Right Arrow = M.
    Last edited by Nathan the noob; 04-01-2010 at 10:16 PM.
    Who needs a signature?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So look at the following:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    int main(void) {
        char choice = ' ';
        while(choice != 'f' && choice != 'm')
        {
            if(kbhit())
            {
                choice = getch();
                if(choice == 0)
                {
                    /*You keep saying you don't want this key SO DON'T READ IT IN
                      choice = getch(); */
                    getch(); /*to discard the special key*/
                }
                printf("choice = %d", choice); /*print out code value to see it*/
            }
            if(isalpha(choice))
            {
                choice = tolower(choice);
            }
        }
    
        return 0;
    }
    This will print the actual codes you are getting. The getch() page on MSDN says that it can return 0x0 OR 0xE0 for a special key. If the latter, you'll probably see "choice = -32choice=102" or whatever. So you would need to check both in that case.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Code:
    while(choice != 'f' && choice != 'm')
                             {
                                 if(kbhit())
                                 {
                                          choice = getch();
                                          if(choice == 0)
                                          {
                                          	getch();
                                          }
                                 }
    			     printf("choice = %d", choice);
                                 if(choice == -32 && choice == 75 || choice == -32 && choice == 73 ||
    				choice == -32 && choice == 72 || choice == -32 && choice == 71 ||
    				choice == -32 && choice == 77 || choice == -32 && choice == 79 ||
    				choice == -32 && choice == 80 || choice == -32 && choice == 81 ||
    				choice == -32 && choice == 83)
                                 {
                                 	choice = '-';
                                 }
                                 cout<<"\b\b\b   \b\b\b";
                                 cout <<choice;
                                 if(isalpha(choice))
                                 {
                                    choice = tolower(choice);
                                 }
    }
    For some reason i still output letters.
    Last edited by Nathan the noob; 04-01-2010 at 10:54 PM.
    Who needs a signature?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    choice can't possibly be both -32 and 75 at the same time, can it?
    Code:
                             while(choice != 'f' && choice != 'm')
                             {
                                 if(kbhit())
                                 {
                                          choice = getch();
                                          if(choice == 0 || choice == -32)
                                          {
                                            /*You keep saying you don't want this key SO DON'T READ IT IN
                                          	choice = getch(); */
                                            getch(); /*to discard the special key*/
                                          }
                                 }
                                 if(isalpha(choice))
                                 {
                                    choice = tolower(choice);
                                 }
                             }

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Oh but above you said i would need to check both incase. And Lol i just made it so when ch = -32 ch = '-' and that worked .

    When is ch not 0 and -32? PS thank you very much .
    Last edited by Nathan the noob; 04-01-2010 at 11:05 PM.
    Who needs a signature?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09: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

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