Solved

Keyboard Input

Posted on 2004-08-31
3
261 Views
Last Modified: 2010-04-01
How do I get a console program to respond to, let's say CTRL-A, CTRL-H, etc etc?  I'm using "switch" to respond to keyboard commands (snippet is below) so if I can incorporate it into that it'd be great!  Also, how do I find out the various methods used to identify keyboard input?  I notice some people use 0x00, 0x46 as key values rather than "13", "65", etc.

switch (key)
{
case 13: <do this>;
break;
case 65: <do that>;
break;
}

Thanks,
Tony
0
Comment
Question by:fattumsdad
3 Comments
 
LVL 22

Expert Comment

by:grg99
Comment Utility
Depends on the OS. You'll have to turn off the usual line-buffering input mods, see your compiler manuals for something like "raw mode input".

0
 
LVL 11

Accepted Solution

by:
bcladd earned 300 total points
Comment Utility
On MS Windows (VS 2003 .NET), you can use conio.h to have access to getch(). That will return a character code for each key pressed.

On XP Pro (and most DOS derived consoles) you should easily get the CTRL charactaters: Ctrl-A returns character 1 (0x01, 1, 001) and Ctrl-B returns character 2 (0x02, 2, 002) and so on.

As to how to specify the characters, using hex, decimal, or octal is basically the same thing. Since the result of getch() is based on the character typed, it should work with different keyboard arrangements (though you will have to examine what happens with character codes beyond the standard, US English, alphabet).

To see what keys generate what code you can try this code:

#include <conio.h>
using namespace std;

int main()
{
  char ch;
  while (ch = getch()) {

    cout << (int)ch << endl;
    if (ch == 'X') break;
  }
  return 0;
}

Hope this helps, -bcl
0
 
LVL 1

Author Comment

by:fattumsdad
Comment Utility
bcl,

I appreciate the help, and the code works great.  Do you know of a website where I can get a list of hex, decimal, and octal characters and which keys they correspond to?

Regards,
Tony
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now