GetKey function

Ok i updated my original getkey function.
Now it returns ascii if you press a character and virtualkeycode if you press something else;
Examples coming soon !
Here is the functions(you need windows.h for it to work)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
#include <windows.h>
TCHAR GetKey() //returns ASCII code if you press a character(a, ? , * ,j, ~ , and so on), and
{              //returns Virtual key code if you dont press a character( VK_UP(up arrow),VK_LEFT(left arrow),VK_SPACE(space), and so on)
INPUT_RECORD InputRecord;
DWORD Writtten;
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    while(true)
    {
        ReadConsoleInputA(hStdIn,&InputRecord,1,&Writtten);
        if(InputRecord.EventType==KEY_EVENT&&InputRecord.Event.KeyEvent.bKeyDown) break;
    }
    char ASCII=InputRecord.Event.KeyEvent.uChar.AsciiChar;
    if(ASCII) return ASCII;
    return InputRecord.Event.KeyEvent.wVirtualKeyCode;
}
int main()
{
    char a;
    cout<<"Press Control Key to Exit,char 'n' to say Hello World !"<<endl;
    while(true)
    {
    a=GetKey();
    if(a==VK_CONTROL) break;
    if(a=='n') cout<<"Hello World !\n";
    }
    cout<<"\nPress any Key to continue . . .\n";
    GetKey();
    return 0;
}
Last edited on
You already had a thread here:
http://www.cplusplus.com/forum/beginner/121585/
Please don't start a new thread like this.
Finally a comment :P and yes sorry for double posting .
Ok i updated it also added examples.
Topic archived. No new replies allowed.