win32 api doesn't work on non-Windows platforms, but this code snippet will work with some possible mods to the defines at the top.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You mean like this
if (KeyStroke == 0 || KeyStroke == -32)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
getch() will return either 0 or 224 when you press special keys like F1-F10 and arrow keys. If it doesn't return one of those two values then you just pressed a normal key. After getting either 0 or 224 you have to call getch() again, then convert that value to something with will not duplicate normal keys. I've seen two different methods of conversion, either can be used as long as you use them consistently.make the keystroke value negative. Such as keystroke = -getch();
Add 255 to the value such as keystroke = getch() + 255;
Run this little program and press several keys to find out how it works.
#include <stdio.h>
#include <conio.h>
int main()
{
int keystroke;
while(1)
{
keystroke = getch();
printf("keystroks = %d\n", keystroke);
if( keystroke == 0 || keystroke == 224)
keystroke = getch() + 255;
printf("keystroks = %d\n", keystroke);
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
In the program you posted you will have to define all the special keys to have different values -- you can't use those values because they conflict with normal keys.
Then just delete lines 37-49 because they are all wrong. You won't get any of those values on the first call to getch().
The if statement on line 51 also needs to check for value of 224 because thats what is returned when an arrow key is pressed.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
As I said before, when a special key is pressed add the value 255 to the second call to getch() to make the key unique from all other keys on the keyboard. That way your program will always be able to distinguish F1 key from the semicolon, which also has key code of 59.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Here is what I mean. Note that variable KeyStroke must be int, not char because it will have a value > 255 for special keys.
#include <iostream>
#include <conio.h>
/* This program shows how to pick up the scan codes from a keyboard */
/* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/
#define PAGE_UP (73+255)
#define HOME (71+255)
#define END (79+255)
#define PAGE_DOWN (81+255)
#define UP_ARROW (72+255)
#define LEFT_ARROW (75+255)
#define DOWN_ARROW (80+255)
#define RIGHT_ARROW (77+255)
#define F1 (59+255)
#define F2 (60+255)
#define F3 (61+255)
#define F4 (62+255)
#define F5 (63+255)
#define F6 (64+255)
#define F7 (65+255)
#define F8 (66+255)
#define F9 (67+255)
#define F10 (68+255)
using namespace std;
int main()
{
int KeyStroke;
cout << "Press Escape to quit." << endl << endl;
do
{
KeyStroke = getch();
if( KeyStroke == 0 || KeyStroke == 224)
KeyStroke = getch()+255;
switch (KeyStroke)
{
case F1:
cout << "F1" << endl;
break;
case F2:
cout << "F2" << endl;
break;
case F3:
cout << "F3" << endl;
break;
case F4:
cout << "F4" << endl;
break;
case F5:
cout << "F5" << endl;
break;
case F6:
cout << "F6" << endl;
break;
case F7:
cout << "F7" << endl;
break;
case F8:
cout << "F8" << endl;
break;
case F9:
cout << "F9" << endl;
break;
case F10:
cout << "F10" << endl;
break;
default:
cout << "Some other key." << KeyStroke << '\n';
}
}while (KeyStroke != 27); // 27 = Escape key
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343