Duplicate scancodes using _getch()

Apr 6, 2009 at 7:23am
Hi guys,

Using the function _getch(), I get the same scancode for both the F12 key and Control-Page Up.

To me this appears to be a bug in _getch().

Is this a known bug, or should I be using a different function to read one key from stdin? Or have I made a mistake somewhere?

Here's an example of what I'm talking about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "std_lib_facilities.h" //From the Bjarne Stroustrup book...
#include <conio.h>
using namespace std;

int main()
{
  char ch;
  char ch_ext;

  ch = ' ';
  ch_ext = ' ';

  while (true)
  {
    if(_kbhit()) 
    {
      ch = _getch();
      if(ch == -32)
      {
        ch_ext = _getch();
        switch(ch_ext)
        {
        case -122: //F12 also Ctl-Page Up ????
          cout << "Maybe the F12 key, or Control-Page up was hit. Who knows?\n";
          break;
        }
      }
      else if(ch == 13) //Quit program.
      {
        return 0;
      }
    }
  }
  return 0;
}


For both F12 and Control-Page Up _getch() returns -122 (gotta love those signed char types :) )

This has so far been the only case where I've found duplicate scancodes, the compiler caught it when I tried to compile my project with the same "case -122:" statement.


Thanks :)
Last edited on Apr 6, 2009 at 7:24am
Apr 6, 2009 at 11:40am
I know getch returns an int, maybe try using an int instead, it might be overflowing and getting cut off.
Apr 11, 2009 at 7:01am
Sorry about the late reply, I'm visiting family and am away from the house. :)

Anyways, I tried what you suggested and now the scan codes come back as what I would consider a sane value (no more negative numbers). But it still does the same thing (although I had to change my if and switch (case) statements to reflect the change from char to int).

Thanks for the suggestion though, it was a good idea.


The new test code using int's:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

int main()
{
  int ch;
  int ch_ext;

  ch = ' ';
  ch_ext = ' ';

  while (true)
  {
    if(_kbhit()) 
    {
      ch = _getch();
      if(ch == 224)
      {
        ch_ext = _getch();
        switch(ch_ext)
        {
          case 134:	//F12 also Ctl-Page Up ????
          cout << "Maybe the F12 key, or Control-Page up was hit. Who knows?\n";
          break;
        }
      }
      else if(ch == 13)	//Quit program.
      {
        return 0;
      }
    }
  }
return 0;
}


Is there a different library that I should be using?

Thanks again.

Last edited on Apr 11, 2009 at 7:01am
Apr 11, 2009 at 2:52pm
I've been searching for web pages on keyboard scan codes and make/break codes but I haven't found a simple, definitive page.

It's been a long time since I did any of this stuff, but one solution that codes to mind is that you need to keep the status of the Ctrl/Alt/Shift keys in your program. Your program then has a couple of tables, one that maps scan codes to keys when no modifier keys are pressed, one that maps when Ctrl is pressed, etc.

Apr 12, 2009 at 7:03am
I'm thinking that it has something to do with the newer keyboards (i.e. Windows keyboards).

It's not a big deal since (as the programmer) I can choose to use one or the other of the keys in my program. I'll just avoid using both in one program.

This issue really just sprang up because I was teaching myself C++ and I didn't like how cin, scanf, getc(har) worked. Or in other words, I got sidetracked. :)

If nothing else, I've got a pretty decent chunk of code that I can use as a function to allow me to read characters from the keyboard one at a time.

I'm interested is learning how I can keep track of the Ctrl/Alt/Shift keys in my program, I'll have to do some research on this.


Thanks for all of the help guys, I do appreciate it.
Apr 14, 2009 at 2:22pm
Okay I did a bit of checking around, and I guess what you are referring to is detecting Ctrl/Alt/Shift keys within Windows.

That's fine as far as that goes, and it's what I'll probably use eventually if I develop anything for the Windows side of the world (which is very likely).

I recall writing something in QuickBASIC years ago that would read the state of the keyboard flags. I'll see if I can dig it up once I get home.
Topic archived. No new replies allowed.