Quels sont les paramètres d'un Hook clavier

Lord_Patoche Messages postés 196 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 1 mai 2009 - 8 janv. 2005 à 01:54
Lord_Patoche Messages postés 196 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 1 mai 2009 - 9 janv. 2005 à 00:09
bonjour et meilleurs voeux.Je viens de créer une DLL contenant un hook interceptant les messages du clavier (WH_KEYBOARD). Losrque l'utilisateur presse une touche, je veux que cette touche soit enregistrée dans un fichier. Mon programme déclenchant l'écriture de chacun des caractère dès que la valeur de nCode est supérieure à 0, je rencontre un problème. L'écriture s'effectue lorsque une touche est enfoncée, mais aussi lorsqu'elle est relachée. Il faudrait pour mon exemple que l'écriture ne se face qu'en cas de touche enfoncée (comme l'évènement WM_KEYDOWN). Quel paramètre de la fonction ProcHook(nCode, wParam, lParam) contient le message d'évènement envoyé et sous quelle forme ? Si j'écris un code tel que if(nCode WM_KEYDOWN) cela ne fonctionne pas. De même si j'écris (if wParam WM_KEYDOWN).

Merci d'avance.

LaPatoshe

4 réponses

cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
8 janv. 2005 à 08:39
tiré de la MSDN:

KeyboardProc Function

<HR SIZE=1>
</HR>
The KeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed.
The HOOKPROC type defines a pointer to this callback function. KeyboardProc is a placeholder for the application-defined or library-defined function name.




Syntax


LRESULT CALLBACK KeyboardProc(

int code,
WPARAM wParam,
LPARAM lParam
);


Parameters



* code : [in] Specifies a code the hook procedure uses to determine how to process the message. If code is less than zero, the hook procedure must pass the message to the Keystroke Message Flags. This parameter can be one or more of the following values.
* : 0-15
Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key.
* : 16-23
Specifies the scan code. The value depends on the OEM.
* : 24
Specifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0.
* : 25-28
Reserved.
* : 29
Specifies the context code. The value is 1 if the ALT key is down; otherwise, it is 0.
* : 30
Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
* : 31
<DD>Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
</DD></DL>
Return Value



If code is less than zero, the hook procedure must return the value returned by CallNextHookEx.


If code is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.


Remarks



An application installs the hook procedure by specifying the WH_KEYBOARD hook type and a pointer to the hook procedure in a call to the SetWindowsHookEx function.


Function Information



Header |
Declared in Winuser.h, include Windows.h,
----

Import library |
None,
----

Minimum operating systems |
Windows 95, Windows NT 3.

puis apres avoir regarder les "KeyStoreFlag", on y voit apparaitre WM_KEYUP et WM_KEYDOWN... donc se trouve dans lParam de la fonction...
Bob...

"La chance accorde ses faveur aux esprits avertis..."
0
Lord_Patoche Messages postés 196 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 1 mai 2009 3
8 janv. 2005 à 18:46
Salut, et merci. Si j'ai bien compris, donc, WH_KEYBOARD du Hook récupère les messages de touche enfoncée ou relachée du clavier. nCode donne l'état du clavier (touche en action ou non). Puis, c'est wParam qui donne le code de la touche enfoncée, et lParam indique combien d'itération cette touche a envoyé lors de sa pression ou de sa relache.
Seulement, existe t il un moyen de savoir avec nCode, wParam ou lParam si la touche est enfoncée ou relachée ? Ce renseignemnt serait il par hasard inscrit dans l'octet de poid fort ou faible de wParam ou de lParam ?
Pour info, le code de touche de wParam n'est pas reconnu sur ma bécane lorsque j'utilise les codes des lettres (ex VK_N ou VK_T) Si j'utilise les VK des touches de fonction, ça marche, saurais tu d'où cela vient ?
LaPatoshe
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
8 janv. 2005 à 20:23
en fait lParam dit si la touche est enfoncé ou relaché (WM_KEYUP et WM_KEYDOWN) pour la touche.


Regarde ce bout de code simule l'appuie de deux touche quand on tape la touche TAB:


// code hook (simul l'appui sur Win + M)


LRESULT CALLBACK kbdWinAmp(
int nCode, WPARAM wParam, LPARAM lParam)


{



if(nCode >= 0)
// kan pression sur touche


{



if(wParam == VK_TAB)


{


keybd_event(VK_LWIN, 0, 0, 0);


keybd_event(0x4D, 0, 0, 0);


keybd_event(0x4D, 0, KEYEVENTF_KEYUP, 0);


keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);


}


}



return CallNextHookEx(hhk, nCode, wParam, lParam);


}

Bob...
"La chance accorde ses faveur aux esprits avertis..."
0
Lord_Patoche Messages postés 196 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 1 mai 2009 3
9 janv. 2005 à 00:09
Merci beaucoup, ça fonctionne

LaPatoshe
0
Rejoignez-nous