Solved

Keyboard Input

Posted on 2004-08-31
3
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 clear a vector as well as how to detect empty vectors in C++.

690 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