[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Keyboard Input

Posted on 2004-08-31
3
Medium Priority
?
277 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 1200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

650 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