Reputation: 255
l want to know how i can represent keys like enter shift etc in c programming language. is there any binary values for those keys? as i can represent esc key with a help of binary value 27. for example:
int main()
{
int x;
char ch;
while(1)
{
ch=getch();
if(ch==27)
{
printf("esc key pressed");
break;
}
else
{
printf("%c key pressed\n",ch);
}
}
}
in my program if i press esc it will break. now if i want to break with any other keys like enter, shift, alt how to do it ?
Upvotes: 2
Views: 2157
Reputation: 19333
Most of this would be platform dependent. However, some keys can be detected by using the keyboard scan codes and the following example program:
#include<stdio.h>
int main()
{
int ch;
int ascii[10];
int count=1;
ch=getchar();
ascii[0]=ch;
while (1) //Loop used for storing successive ASCII values
{
ascii[count]=getchar();
if (ascii[count]==10) break;
count++;
}
if (27==ascii[0] && 79==ascii[1] && 80==ascii[2]) printf("F1 is pressed");
if (27==ascii[0] && 79==ascii[1] && 81==ascii[2]) printf("F2 is pressed");
if (27==ascii[0] && 79==ascii[1] && 82==ascii[2]) printf("F3 is pressed");
if (27==ascii[0] && 91==ascii[1] && 72==ascii[2]) printf("HOME key is pressed");
return 0;
}
If you want a more useful example without having to hit ENTER after each keystroke, you could try disabling input buffering and enabling raw mode.
Upvotes: 2
Reputation: 23218
I want to know how i can represent keys like enter shift etc in c programming language...
As others have said, this is OS dependent.
This function:
short GetAsyncKeyState(int key);
( For Windows programming ) can be used in conjunction with the following #defines in WinUser.h to determine the current AND recent states of a key:
#define VK_CLEAR 0x0C
#define VK_RETURN 0x0D //AKA enter
#define VK_SHIFT 0x10
#define VK_CONTROL 0x11
#define VK_MENU 0x12
#define VK_PAUSE 0x13
#define VK_CAPITAL 0x14
#define VK_KANA 0x15
#define VK_HANGEUL 0x15 /* old name - should be here for compatibility */
#define VK_HANGUL 0x15
#define VK_JUNJA 0x17
#define VK_FINAL 0x18
#define VK_HANJA 0x19
#define VK_KANJI 0x19
#define VK_ESCAPE 0x1B
#define VK_CONVERT 0x1C
#define VK_NONCONVERT 0x1D
#define VK_ACCEPT 0x1E
#define VK_MODECHANGE 0x1F
#define VK_SPACE 0x20
#define VK_PRIOR 0x21
#define VK_NEXT 0x22
#define VK_END 0x23
#define VK_HOME 0x24
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define VK_SELECT 0x29
#define VK_PRINT 0x2A
#define VK_EXECUTE 0x2B
#define VK_SNAPSHOT 0x2C
#define VK_INSERT 0x2D
#define VK_DELETE 0x2E
#define VK_HELP 0x2F
Upvotes: 1