in c programming,
how can I capture keys like "page down", "ctrl+?", etc. using standard library?
Printable View
in c programming,
how can I capture keys like "page down", "ctrl+?", etc. using standard library?
I think that can't capture the 'page down',
any other method?
> I think that can't capture the 'page down',
Think again then
Extended keys arrive as two bytes - the first being zero
Now call this in a loop and print out the return value as you press each key, and study what is going on.Code:int mygetch ( void ) {
int ch = getch();
if ( ch == 0 ) ch = 256 + getch();
return ch;
}
If you are coding in DOS you write and interrupt 9 handler. If it's in Windows you simply check for the correct VK code - after masking out the correct bits. Check the Windows SDK on that.
As for DOS (C++):
Code:
int keys[128];
#define Keydown(scan_code) (if keys[scan_code]?1:0)
interrupt *OldHandler(...)=0;
int main(void)
{
//Register atexit function
atexit(UninstallHandler);
....
....
return (0);
}
interrupt NewKeyHandler(...)
{
int x=inportb(0x60);
if (x>127)
{
keys[x - 127]=0;
} else keys[x]=1;
}
void InstallHandler(void)
{
getvect(0x09,OldHandler);
setvect(0x09,NewKeyHandler);
}
void UninstallHandler(void)
{
setvect(0x09,OldHandler);
}
thanks for all of you,
to Salem,
I know EXTENDED KEY needs two bytes,
I also find a link full of these extended keys,as below:
http://www.teletechnics.co.nz/refere...cii_codes.html
but, How can I capture them?
like:
Code:char ch;
ch=getch();
if(ch=='????')
{
do sth.
}
> I know EXTENDED KEY needs two bytes,
But your example code says that you don't
Read my code again - see how extended key codes get 256 added to them
So from your handy table
You can writeCode:59 3B 073 00111011 F1
Code:int ch = mygetch( );
if ( ch == 256 + 59 ) {
// F1 pressed
}
get ctrl and key E or left eg.
unfortuately if you want to check if it's capital letters inputted, u must check for the shift or caps button.Code:#include <windows.h>
// ... code
if( GetAsyncKeyState(VK_CONTROL) &&
GetAsyncKeyState('E') )
{
// do what ever
}
else if(GetAsyncKeyState(VK_LEFT) )
{
// do what ever
}
all the virtual keys are here.
http://msdn.microsoft.com/library/de...alKeyCodes.asp
obviously this is only for windows :)
somewhere in there is
VK_PRIOR (21)
PAGE UP key
VK_NEXT (22)
PAGE DOWN key
also if ur using win NT/XP/2000 these work aswell
VK_LSHIFT Left-shift key.
VK_RSHIFT Right-shift key.
VK_LCONTROL Left-control key.
VK_RCONTROL Right-control key.
VK_LMENU Left-menu key.
VK_RMENU Right-menu key.
I am sorry to trouble you again, Salem
Your method is working well for keys F1--F12,
but the rest don't work.
what's wrong?
Code:int mygetch ( void ) {
int ch = getch();
if ( ch == 0 ) ch = 256 + getch();
return ch;
}
main()
{
while(1)
{
if(mygetch()==256+81) // 81 is PageDown
break;
}
}
Read the help on _getch(). Everything you needed to know is right there. Just have to read it.
ggCode:int mygetch(void)
{
int ch = getch();
if (ch == 0)
{
ch = 256 + getch();
}
else if (ch == 0xE0)
{
ch = 512 + getch();
}
return ch;
}
The Problem has been solved :)
thanks to all of you,
Special thanks to Salem and CodePlug.
to CodePlug and Salem,
I think plus 512 and 256 is not needed in the code anymore.
The Final Correct Code:
Code:int mygetch(void)
{
int ch = getch();
if (ch == 0 || ch == 0xE0)
{
ch = getch();
}
return ch;
}
main()
{
while(1)
{
if(mygetch()==81) //81 is PageDown
break;
}
}
> if(mygetch()==81) //81 is PageDown
And how will you tell the difference if the user presses 'Q' instead?
Salem,
thank you for giving me such an inspirational hint.
I think I get it.
You mean 256 is necessary, right?
then the code must go this way:
-----------------------------------------------------------------Code:int mygetch(void)
{
int ch=getch();
if(ch==0||ch==0xE0)
{
ch=256+getch();
}
return ch;
}
main()
{
int ch=mygetch();
switch(ch)
{
case 81:
printf("you press 'Q' ");
break;
case 256+81:
printf("you press 'page down' ");
break;
}
getch();
}
by the way,
I found that the arrow keys has no corresponding ASCII.
but I wrote some codes to try it, and found that they also has two-character , and the first is -32.
So I try to capture it the same way as above(simply replace 0 as -32).
It works all right.
Does it a correct way to capture arrow keys?
-------------------
how about the CTRL SHIFT and ALT ? I don't know how to try them.
-------------------
wish you can answer my boring questions altogether
:)
last,
what's this below? another way of capture keys?
#define SHIFT_R 0x0001
#define SHIFT_L 0x0002
#define CTRL 0x0004
#define ALT 0x0008
#define SCROLL_LOCK_ON 0x0010
#define NUM_LOCK_ON 0x0020
#define CAPS_LOCK_ON 0x0040
thank you so much.