getch - how to use my_getch in c++? -
i created getch-liked function using windows.h-getkeystate , working when in loops same char lots of times. sould do?
i using gcc 4.9.2 code blocks windows 10 .
my code :
#include <windows.h> #include <iostream> bool pressed(int key){return getkeystate(key)&0x8000;} int my_getch(){ while(true){ bool shift=pressed(vk_shift); for(char a='a'; a<='z'; a++) if(pressed(a)) return shift||getkeystate(vk_capital)?a:a-'a'+'a'; if(pressed(vk_oem_3)) return shift?'~':'`'; if(pressed(vk_oem_4)) return shift?'{':'['; if(pressed(vk_oem_6)) return shift?'}':']'; if(pressed(vk_oem_5)) return shift?'|':'\\'; if(pressed(vk_oem_1)) return shift?':':';'; if(pressed(vk_oem_7)) return shift?'"':'\''; if(pressed(vk_oem_comma)) return shift?'<':','; if(pressed(vk_oem_period)) return shift?'>':'.'; if(pressed(vk_oem_2)) return shift?'?':'/'; if(pressed('1')) return shift?'!':'1'; if(pressed('2')) return shift?'@':'2'; if(pressed('3')) return shift?'#':'3'; if(pressed('4')) return shift?'$':'4'; if(pressed('5')) return shift?'%':'5'; if(pressed('6')) return shift?'^':'6'; if(pressed('7')) return shift?'&':'7'; if(pressed('8')) return shift?'*':'8'; if(pressed('9')) return shift?'(':'9'; if(pressed('0')) return shift?')':'0'; if(pressed(vk_oem_minus)) return shift?'_':'-'; if(pressed(vk_oem_plus)) return shift?'+':'='; if(pressed('\b')) return '\b'; if(pressed(vk_divide)) return '/'; if(pressed(vk_multiply)) return '*'; if(pressed(vk_add)) return '+'; if(pressed(vk_subtract)) return '-'; if(pressed('\t')) return '\t'; if(pressed(' ')) return ' '; for(int i=vk_numpad0; i<=vk_numpad9; i++) if(pressed(i)) return i-vk_numpad0+'0'; if(pressed(vk_decimal)) return '.'; if(pressed('\r')) return '\n'; } } int winmain(hinstance, hinstance, char*, int){ while(true) std::cout<<my_getch()<<'\r'; return 0;}
looking @ docs getkeystate:
the key status returned function changes thread reads key messages message queue. status not reflect interrupt-level state associated hardware. use getasynckeystate function retrieve information.
which suggests should using getasynckeystate instead. docs:
determines whether key or down @ time function called, , whether key pressed after previous call getasynckeystate.
wiki
Comments
Post a Comment