Kevin Dong
Kevin Dong

Reputation: 5359

How to know the key which has been pressed by users?

I have a console application in C.

I am trying to detect which key has been pressed by users:

if (...)
    printf ("Shift + Right Arrow");
if (...)
    printf ("Right Arrow");
if (...)
    printf ("中"); // (Big5(CP950): 0xA4 0xA4)
if (...)
    printf ("文"); // (Big5(CP950): 0xA4 0xE5)
....

How do I detect which has been pressed by users?

Upvotes: 0

Views: 103

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>
#include <Windows.h>
#include <Winuser.h>

BOOL isPressShift(){
    return (SHORT)0x8000 & GetKeyState( VK_SHIFT );
}

BOOL isPressRightArrow(){
    return (SHORT)0x8000 & GetKeyState( VK_RIGHT );
}


int main(void){
//  ... do something
    if(isPressShift() && isPressRightArrow())
        printf("Shift + Right Arrow\n");
    if(isPressRightArrow())
        printf("Right Arrow\n");
    return 0;
}

Upvotes: 2

norlesh
norlesh

Reputation: 1841

You want to check the virtual keys heres a list

Upvotes: 0

Related Questions