HI ! Im using your fantastic project, pyHook, but I get a
problem with my spanish keyboard. Perhaps you know what Im
talking about, but I explain it to you: when I use dead keys
like accents I dont get the correct result in the text
editor, i.e: á appears like ´´a. I have read a little the
code but I get lost because there is code in C++, and I dont
know how it works. It would be great if you could solve it
or saying to me a clue to to solve it myself.
thanks and congratulations for your excelent work.
you can contact me in jeanmichelem at gmail.com
best regards
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2010-01-22
Hello!
I am also using a similar (german) keyboard, and I have that problem too.
Is there any solution about this?
Otherwise this approach seems obsolete, when it is not possible
to use accents or circumflex ( î Î â Â ) characters.
Is it possible to ignore certain keystrokes before they were "hooked" ?
All help is very much appreciated!
Thank and regards
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Unfortunately, I didn't get any response from the developers. I was trying to do what you want to do, but it is a hard task, because the code is a mix between Python and C++, and I just decided not to lose time much time on it, and create my own Keylogger. To do that, I wrote a program using C++ and the win32 API. It works fine, and have (more or less) the same funtionality that pylogger, and the best is that it is not a problem to create the exe file, and it is faster and more efficient as you can imagine. I did that several months ago, but it is not completely tested, so it would be great if you want to help me in that issue. If you are interested, contact me at: jeanmichelem@gmail.com, and I will share my keylogger sources with you.
Best Regards.
Juan.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi there,
I have a sort of fix for this bug, but be warned: it's not thoroughly tested. It enables the transmission of dead key characters to the system, but does not allow your app to catch them completely (for an "ô", you'll get an "O"). Anyway, it fits my needs (catch the F12 key and allow french users to type what they need). This solution comes from there: http://batcheur.tuxfamily.org/codes-sources/MyKeyloggerV2.txt
Here is the modified C file (cpyHook.i):
%modulecpyHook%includetypemaps.i%{#define_WIN32_WINNT0x400#include"windows.h"PyObject*callback_funcs[WH_MAX];HHOOKhHooks[WH_MAX];BYTEkey_state[256];%}#ifdef SWIGPYTHON%typemap(in)PyObject*pyfunc{if(!PyCallable_Check($input)){PyErr_SetString(PyExc_TypeError,"Need a callable object");returnNULL;}$1=$input;}#endif%init%{memset(key_state,0,256);memset(callback_funcs,0,WH_MAX);memset(hHooks,0,WH_MAX);PyEval_InitThreads();// get initial key statePy_BEGIN_ALLOW_THREADSkey_state[VK_NUMLOCK]=(GetKeyState(VK_NUMLOCK)&0x0001)?0x01:0x00;key_state[VK_CAPITAL]=(GetKeyState(VK_CAPITAL)&0x0001)?0x01:0x00;key_state[VK_SCROLL]=(GetKeyState(VK_SCROLL)&0x0001)?0x01:0x00;Py_END_ALLOW_THREADS%}%wrapper%{unsignedshortConvertToASCII(unsignedintkeycode,unsignedintscancode);voidUpdateKeyState(unsignedintvkey,intmsg);// from http://batcheur.tuxfamily.org/codes-sources/MyKeyloggerV2.txtintIsDeadKey(intvkcode){// MapVirtualKey : http://msdn.microsoft.com/en-us/library/ms646306%28VS.85%29.aspxreturn(MapVirtualKey(vkcode,0)==26)?1:0;}LRESULTCALLBACKcLLKeyboardCallback(intcode,WPARAMwParam,LPARAMlParam){PyObject*arglist,*r;PKBDLLHOOKSTRUCTkbd;HWNDhwnd;PSTRwin_name=NULL;unsignedintascii=0;staticintwin_len;staticlongresult;longpass=1;PyGILState_STATEgil;// Traduction de la toucheWORDChar=0;// avec ToAsciicharsKeyName[MAX_PATH]="";// avec GetKeyNameTextDWORDdwMsg=1;BYTEKeyState[256];// Etat du clavierstaticintbDeadKey=0;// uncomment this next bit if you do not want to process events like "ctl-alt-del"// and other events that are not supposed to be processed// as per msdn documentation:// http://msdn.microsoft.com/en-us/library/ms644985(VS.85).aspx// if message code < 0, return immediately//if(code<0)// CallNextHookEx(hHooks[WH_KEYBOARD_LL], code, wParam, lParam);// get the GILgil=PyGILState_Ensure();// cast to a keyboard event structkbd=(PKBDLLHOOKSTRUCT)lParam;// get the current foreground window (might not be the real window that received the event)hwnd=GetForegroundWindow();// grab the window name if possiblewin_len=GetWindowTextLength(hwnd);if(win_len>0){win_name=(PSTR)malloc(sizeof(char)*win_len+1);GetWindowText(hwnd,win_name,win_len+1);}/////////////////////////////////////////// from http://batcheur.tuxfamily.org/codes-sources/MyKeyloggerV2.txtif(code<0||code==HC_NOREMOVE)//Voir l'API Win32returnCallNextHookEx(hHooks[WH_KEYBOARD_LL],code,wParam,lParam);if(IsDeadKey(kbd->vkCode)){bDeadKey=1;returnCallNextHookEx(hHooks[WH_KEYBOARD_LL],code,wParam,lParam);}//Remise a 0 de KeyStatememset(KeyState,0,sizeof(KeyState));// GetKeyboardState : http://msdn.microsoft.com/en-us/library/ms646299%28VS.85%29.aspxif(GetKeyboardState(KeyState)){if(!bDeadKey){// ToAscii : http://msdn.microsoft.com/en-us/library/ms646316%28VS.85%29.aspxToAscii((UINT)kbd->vkCode,(UINT)((lParam<<8)>>24),KeyState,&Char,0);//myfprintf((char*)&Char);ascii=&Char;}else{//Ajoute la touche concernée ainsi que l'état du flag (8 : vkCode; 16 : scanCode; 24 : flags; 32 : time)dwMsg+=(kbd->scanCode<<16);// Pour avoir le scanCodedwMsg+=(kbd->flags<<24);// Pour avoir le flag//Retourne sa traductionGetKeyNameText(dwMsg,sKeyName,MAX_PATH);//myfprintf((char*)sKeyName);ascii=sKeyName;bDeadKey=0;}}/////////////////////////////////////////// convert to an ASCII code if possible//ascii = ConvertToASCII(kbd->vkCode, kbd->scanCode);// pass the message on to the Python functionarglist=Py_BuildValue("(iiiiiiiz)",wParam,kbd->vkCode,kbd->scanCode,ascii,kbd->flags,kbd->time,hwnd,win_name);r=PyObject_CallObject(callback_funcs[WH_KEYBOARD_LL],arglist);// check if we should pass the event on or notif(r==NULL)PyErr_Print();elsepass=PyInt_AsLong(r);Py_XDECREF(r);Py_DECREF(arglist);// release the GILPyGILState_Release(gil);// free the memory for the window nameif(win_name!=NULL)free(win_name);// decide whether or not to call the next hookif(pass){UpdateKeyState(kbd->vkCode,wParam);result=CallNextHookEx(hHooks[WH_KEYBOARD_LL],code,wParam,lParam);}else{// return a non-zero to prevent further processingresult=42;}returnresult;}LRESULTCALLBACKcLLMouseCallback(intcode,WPARAMwParam,LPARAMlParam){PyObject*arglist,*r;PMSLLHOOKSTRUCTms;HWNDhwnd;PSTRwin_name=NULL;staticintwin_len;staticlongresult;longpass=1;PyGILState_STATEgil;// get the GILgil=PyGILState_Ensure();//pass the message on to the Python functionms=(PMSLLHOOKSTRUCT)lParam;hwnd=WindowFromPoint(ms->pt);//grab the window name if possiblewin_len=GetWindowTextLength(hwnd);if(win_len>0){win_name=(PSTR)malloc(sizeof(char)*win_len+1);GetWindowText(hwnd,win_name,win_len+1);}//build the argument list to the callback functionarglist=Py_BuildValue("(iiiiiiiz)",wParam,ms->pt.x,ms->pt.y,ms->mouseData,ms->flags,ms->time,hwnd,win_name);r=PyObject_CallObject(callback_funcs[WH_MOUSE_LL],arglist);// check if we should pass the event on or notif(r==NULL)PyErr_Print();elsepass=PyInt_AsLong(r);Py_XDECREF(r);Py_DECREF(arglist);// release the GILPyGILState_Release(gil);//free the memory for the window nameif(win_name!=NULL)free(win_name);// decide whether or not to call the next hookif(code<0||pass)result=CallNextHookEx(hHooks[WH_MOUSE_LL],code,wParam,lParam);else{// return non-zero to prevent further processingresult=42;}returnresult;}intcSetHook(intidHook,PyObject*pyfunc){HINSTANCEhMod;//make sure we have a valid hook numberif(idHook>WH_MAX||idHook<WH_MIN){PyErr_SetString(PyExc_ValueError,"Hooking error: invalid hook ID");}//get the module handlePy_BEGIN_ALLOW_THREADS// try to get handle for current file - will succeed if called from a compiled .exehMod=GetModuleHandle(NULL);if(NULL==hMod)// otherwise use name for DLLhMod=GetModuleHandle("_cpyHook.pyd");Py_END_ALLOW_THREADS//switch on the type of hook so we point to the right C callbackswitch(idHook){caseWH_MOUSE_LL:if(callback_funcs[idHook]!=NULL)break;callback_funcs[idHook]=pyfunc;Py_INCREF(callback_funcs[idHook]);Py_BEGIN_ALLOW_THREADShHooks[idHook]=SetWindowsHookEx(WH_MOUSE_LL,cLLMouseCallback,(HINSTANCE)hMod,0);Py_END_ALLOW_THREADSbreak;caseWH_KEYBOARD_LL:if(callback_funcs[idHook]!=NULL)break;callback_funcs[idHook]=pyfunc;Py_INCREF(callback_funcs[idHook]);Py_BEGIN_ALLOW_THREADShHooks[idHook]=SetWindowsHookEx(WH_KEYBOARD_LL,cLLKeyboardCallback,(HINSTANCE)hMod,0);Py_END_ALLOW_THREADSbreak;default:return0;}if(!hHooks[idHook]){PyErr_SetString(PyExc_TypeError,"Could not set hook");}return1;}intcUnhook(intidHook){BOOLresult;//make sure we have a valid hook numberif(idHook>WH_MAX||idHook<WH_MIN){PyErr_SetString(PyExc_ValueError,"Invalid hook ID");}//unhook the callbackPy_BEGIN_ALLOW_THREADSresult=UnhookWindowsHookEx(hHooks[idHook]);Py_END_ALLOW_THREADSif(result){//decrease the ref to the Python callbackPy_DECREF(callback_funcs[idHook]);callback_funcs[idHook]=NULL;}returnresult;}voidSetKeyState(unsignedintvkey,intdown){// (1 > 0) ? True : Falseif(vkey==VK_MENU||vkey==VK_LMENU||vkey==VK_RMENU){key_state[vkey]=(down)?0x80:0x00;key_state[VK_MENU]=key_state[VK_LMENU]|key_state[VK_RMENU];}elseif(vkey==VK_SHIFT||vkey==VK_LSHIFT||vkey==VK_RSHIFT){key_state[vkey]=(down)?0x80:0x00;key_state[VK_SHIFT]=key_state[VK_LSHIFT]|key_state[VK_RSHIFT];}elseif(vkey==VK_CONTROL||vkey==VK_LCONTROL||vkey==VK_RCONTROL){key_state[vkey]=(down)?0x80:0x00;key_state[VK_CONTROL]=key_state[VK_LCONTROL]|key_state[VK_RCONTROL];}elseif(vkey==VK_NUMLOCK&&!down){key_state[VK_NUMLOCK]=!key_state[VK_NUMLOCK];}elseif(vkey==VK_CAPITAL&&!down){key_state[VK_CAPITAL]=!key_state[VK_CAPITAL];}elseif(vkey==VK_SCROLL&&!down){key_state[VK_SCROLL]=!key_state[VK_SCROLL];}}voidUpdateKeyState(unsignedintvkey,intmsg){if(msg==WM_KEYDOWN||msg==WM_SYSKEYDOWN){SetKeyState(vkey,1);}elseif(msg==WM_KEYUP||msg==WM_SYSKEYUP){SetKeyState(vkey,0);}}unsignedintcGetKeyState(unsignedintvkey){returnkey_state[vkey];}unsignedshortConvertToASCII(unsignedintkeycode,unsignedintscancode){intr;unsignedshortc=0;Py_BEGIN_ALLOW_THREADSr=ToAscii(keycode,scancode,key_state,&c,0);Py_END_ALLOW_THREADSif(r<0){//PyErr_SetString(PyExc_ValueError, "Could not convert to ASCII");return0;}returnc;}%}unsignedintcGetKeyState(unsignedintvkey);intcSetHook(intidHook,PyObject*pyfunc);intcUnhook(intidHook);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
HI ! Im using your fantastic project, pyHook, but I get a
problem with my spanish keyboard. Perhaps you know what Im
talking about, but I explain it to you: when I use dead keys
like accents I dont get the correct result in the text
editor, i.e: á appears like ´´a. I have read a little the
code but I get lost because there is code in C++, and I dont
know how it works. It would be great if you could solve it
or saying to me a clue to to solve it myself.
thanks and congratulations for your excelent work.
you can contact me in jeanmichelem at gmail.com
best regards
Hello!
I am also using a similar (german) keyboard, and I have that problem too.
Is there any solution about this?
Otherwise this approach seems obsolete, when it is not possible
to use accents or circumflex ( î Î â Â ) characters.
Is it possible to ignore certain keystrokes before they were "hooked" ?
All help is very much appreciated!
Thank and regards
Hi Photon72!
Unfortunately, I didn't get any response from the developers. I was trying to do what you want to do, but it is a hard task, because the code is a mix between Python and C++, and I just decided not to lose time much time on it, and create my own Keylogger. To do that, I wrote a program using C++ and the win32 API. It works fine, and have (more or less) the same funtionality that pylogger, and the best is that it is not a problem to create the exe file, and it is faster and more efficient as you can imagine. I did that several months ago, but it is not completely tested, so it would be great if you want to help me in that issue. If you are interested, contact me at: jeanmichelem@gmail.com, and I will share my keylogger sources with you.
Best Regards.
Juan.
Wasn't this fixed here :
https://sourceforge.net/tracker/index.php?func=detail&aid=1624870&group_id=65529&atid=511317
(I was searching for something else and stumbled on that.)
Is it possible to write the pyhook function into a C++ dll and use that dll from python instead of the API wrapper?
Hi there,
I have a sort of fix for this bug, but be warned: it's not thoroughly tested. It enables the transmission of dead key characters to the system, but does not allow your app to catch them completely (for an "ô", you'll get an "O"). Anyway, it fits my needs (catch the F12 key and allow french users to type what they need). This solution comes from there: http://batcheur.tuxfamily.org/codes-sources/MyKeyloggerV2.txt
Here is the modified C file (cpyHook.i):