visual studio 2010 - C++ ToUnicodeEx() not recognizing shift key unless i have a messgaebox() -



visual studio 2010 - C++ ToUnicodeEx() not recognizing shift key unless i have a messgaebox() -

im having bit of problem writing keyboard hook in c++.

i can read keystrokes im trying using tounicodeex() convert keystrokes when shift key pressed. in code have far have

i = tounicodeex(keyboard.vkcode, keyboard.scancode, (pbyte)&keystate, (lpwstr)&keybuff, sizeof(keybuff) / 16, 0,keyboardlayout); messagebox(mainnhwnd,keybuff, l"message", mb_ok | mb_iconexclamation);

with 'messagebox' line when press shift+2 2 message boxes pops up, first blank shift key, sec shows '@' character. expected.

but if remove messagebox, tounicodeex() function converts keystroke if shift key had not been used. can see either setting breakpoint, nail counter, or outputting character edit box in programme windows.

also when include msgbox line , utilize caplock, letters alter accordingly after remove msgbox line uses state of cap lock @ time programme starts (cap locks on when programm starts, letters capital, vice verse, caps off when programm starts letters small,, if alter cap lock state)

anyone know why hook remembers keyboard state @ start, unless include msgbox?

my hook set like:

thehook = setwindowshookex ( wh_keyboard_ll, (hookproc) keyevent, exe, 0);

and hook callback function is:

dllexport lresult callback keyevent(int ncode, wparam wparam, lparam lparam) { if (ncode>=0) { int = 0; kbdllhookstruct keyboard; wchar keybuff[256]= {0}; if ((wparam == wm_keydown)|| (wparam == wm_syskeydown)||(wparam == wm_syskeyup)) { keyboard = *((kbdllhookstruct*)lparam); if (keyboard.vkcode == vk_return) { += wsprintf (((lpwstr)keybuff + i),l"[return]\r\n"); } else { hkl keyboardlayout = getkeyboardlayout(0); getkeyboardstate((pbyte)&keystate); = tounicodeex(keyboard.vkcode, keyboard.scancode, (pbyte)&keystate, (lpwstr)&keybuff, sizeof(keybuff) / 16, 0,keyboardlayout); messagebox(mainnhwnd,keybuff, l"message", mb_ok | mb_iconexclamation); } if (keybuff>0) { addtoeditbox(keybuff); } } } homecoming callnexthookex(thehook, ncode, wparam, lparam); }

according documentation of tounicodeex function, should provide pointer 256-byte array contains current keyboard state (const byte *lpkeystate). each element (byte) in array contains state of 1 key. if high-order bit of byte set, key down.

before phone call tounicodeex, should set array (pseudocode):

enum keys { shiftkey = 16, // shift controlkey = 17, // ctrl menu = 18, // alt capital = 20, // caps lock }; byte keystate[256]= {0}; if (control key down) keystate[keys::controlkey] = 0x80; if (shift key down) keystate[keys::shiftkey] = 0x80; if (alt key down) keystate[keys::menu] = 0x80; if (shift key down) keystate[keys::shiftkey] = 0x80; if (caps lock on) keystate[keys::capital] = 0x01;

and when keystate array set, can call:

tounicodeex(keyboard.vkcode, keyboard.scancode, (pbyte)&keystate, (lpwstr)&keybuff, sizeof(keybuff) / 16, 0,keyboardlayout);

i have been working tounicodeex function , worked fine. hope help ;)

c++ visual-studio-2010 hook

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -