Solved

Keyboard Input

Posted on 2004-08-31
3
262 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 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

15 Experts available now in Live!

Get 1:1 Help Now