Solved

Keyboard Input

Posted on 2004-08-31
3
264 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
ID: 11951112
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
ID: 11952198
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
ID: 11952603
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

828 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