tigre44
Messages postés
15
Date d'inscription
samedi 20 mai 2006
Statut
Membre
Dernière intervention
19 janvier 2007
16 janv. 2007 à 12:36
J'ai déjà l'exemple de quelqu'un qui commence à enfin avoir des résultats, moi .
Le code permet de bloquer les boutons alt, ctrld, ctrlg, echap, F4, et les touches windows.
Par contre il ne fonctionne pas avec CTRL-ALT-SUPPR ce qui est ettonant.
Enfin le voilà, c'est une modification du premier, appelé par Hook.Hook(). Je retourne 1 à la fonction qui s'occupe de ces touches. Je le poste à suivre (Désolé pour les longues lignes) :
Imports
System.Runtime.InteropServices
Imports
System.Reflection
Module
Hook#
Region
"APIs"
Private
Declare
Function UnhookWindowsHookEx
Lib
"user32" (
ByVal hHook
As
Integer)
As
Integer
Private
Declare
Function SetWindowsHookExClavier
Lib
"user32"
Alias
"SetWindowsHookExA" (
ByVal idHook
As
Integer,
ByVal lpfn
As HookClavierDelegate,
ByVal hmod
As
Integer,
ByVal dwThreadId
As
Integer)
As
Integer
Private
Declare
Function SetWindowsHookExSouris
Lib
"user32"
Alias
"SetWindowsHookExA" (
ByVal idHook
As
Integer,
ByVal lpfn
As HookSourisDelegate,
ByVal hmod
As
Integer,
ByVal dwThreadId
As
Integer)
As
Integer
Private
Declare
Function GetAsyncKeyState
Lib
"user32" (
ByVal vKey
As
Integer)
As
Integer
Private
Declare
Function CallNextHookExClavier
Lib
"user32"
Alias
"CallNextHookEx" (
ByVal hHook
As
Integer,
ByVal nCode
As
Integer,
ByVal wParam
As
Integer,
ByRef lparam
As KBDLLHOOKSTRUCT)
As
Integer
Private
Declare
Function CallNextHookExSouris
Lib
"user32"
Alias
"CallNextHookEx" (
ByVal hHook
As
Integer,
ByVal nCode
As
Integer,
ByVal wParam
As
Integer,
ByRef lparam
As MSLLHOOKSTRUCT)
As
Integer
Private
Declare
Function GetModuleHandle
Lib
"kernel32.dll"
Alias
"GetModuleHandleA" (
ByVal ApplicationFileName
As
String)
As IntPtr#
End
Region#
Region
"Structures et Constantes"
Private
Structure POINT
Private x
As
Integer
Private y
As
Integer
End
Structure
Private
Structure MSLLHOOKSTRUCT
Public pt
As POINT
Public mouseData
As
Integer
Public flags
As
Integer
Public time
As
Integer
Public dwExtraInfo
As
Integer
End
Structure
Private
Structure KBDLLHOOKSTRUCT
Public vkCode
As
Integer
Public scanCode
As
Integer
Public flags
As
Integer
Public time
As
Integer
Public dwExtraInfo
As
Integer
End
Structure
Private
Const HC_ACTION
As
Integer = 0
Private
Const WH_KEYBOARD_LL
As
Integer = 13
Private
Const WH_MOUSE_LL
As
Integer = 14#
End
Region#
Region
"Delegate"
Private
Delegate
Function HookClavierDelegate(
ByVal Code
As
Integer,
ByVal wParam
As
Integer,
ByRef lParam
As KBDLLHOOKSTRUCT)
As
Integer
Private
Delegate
Function HookSourisDelegate(
ByVal nCode
As
Integer,
ByVal wParam
As
Integer,
ByRef lparam
As MSLLHOOKSTRUCT)
As
Integer<MarshalAs(UnmanagedType.FunctionPtr)>
Private dlgClavierCallBack
As HookClavierDelegate<MarshalAs(UnmanagedType.FunctionPtr)>
Private dlgSourisCallBack
As HookSourisDelegate#
End
Region
Private hHookClavier
As
Integer
Private hHookSouris
As
Integer
Private
WithEvents tmrSeconde
As Timers.Timer
Private nTickIdle
As
Integer
Public
Function Hook()
As
Boolean
Dim hInstance
As
IntegerdlgClavierCallBack =
New HookClavierDelegate(
AddressOf ClavierCallBack)dlgSourisCallBack =
New HookSourisDelegate(
AddressOf SourisCallBack)hInstance = GetModuleHandle(Process.GetCurrentProcess.MainModule.ModuleName)
hHookClavier = SetWindowsHookExClavier(WH_KEYBOARD_LL, dlgClavierCallBack, hInstance, 0)
hHookSouris = SetWindowsHookExSouris(WH_MOUSE_LL, dlgSourisCallBack, hInstance, 0)
If ((hHookClavier <> 0)
AndAlso (hHookSouris <> 0)) ThentmrSeconde New Timers.TimertmrSeconde.AutoReset
TruetmrSeconde.Interval = 100
nTickIdle = Environment.TickCount
tmrSeconde.Start()
Return
True
Else
Return
False
End
If
End
Function
Public
Sub UnHook()
If hHookClavier <> 0
ThenUnhookWindowsHookEx(hHookClavier)
End
If
If hHookSouris <> 0
ThenUnhookWindowsHookEx(hHookSouris)
End
IftmrSeconde.Stop()
tmrSeconde.Close()
hHookClavier = 0
hHookSouris = 0
dlgClavierCallBack NothingdlgSourisCallBack
NothingtmrSeconde =
Nothing
End
Sub
Private
Sub tmrSeconde_Elapsed(
ByVal sender
As
Object,
ByVal e
As Timers.ElapsedEventArgs)
Handles tmrSeconde.Elapsed
Dim nTickActuel
As
Integer = Environment.TickCount
'Delai de l'Idle: 20 minute
'Dim frm As Form1 = CType(Form1.ActiveForm, Form1)
'Form1.Text = (nTickActuel - nTickIdle)
If ((nTickActuel - nTickIdle) / (1000 * 60)) >= 15
Then
'On concidère que l'Idle est suffisant pour declencher le processus UnHook()
Stop
'EVENTS()
End
If
End
Sub#
Region
"Fonctions"
Private
Function ClavierCallBack(
ByVal Code
As
Integer,
ByVal wParam
As
Integer,
ByRef lParam
As KBDLLHOOKSTRUCT)
As
Integer
If (Code = HC_ACTION)
Then
'MsgBox(lParam.vkCode) 'Tester les numéros des touches entréesnTickIdle = Environment.TickCount
End
If
'Si une de ces touches est pressée, le code passe à 65 qui correspond à "a".
If lParam.vkCode = 27
Or lParam.vkCode = 46
Or lParam.vkCode = 91
Or lParam.vkCode = 92
Or lParam.vkCode = 115
Or lParam.vkCode = 162
Or lParam.vkCode = 163
Or lParam.vkCode = 164
Or lParam.vkCode = 165
Then
'wParam = 1
'hHookClavier = 1
'Code = 1
'lParam.vkCode = 1
Return 1
End
If
'On transmet la valeur à Windows.
Return (CallNextHookExClavier(hHookClavier, Code, wParam, lParam))
End
Function
Private
Function SourisCallBack(
ByVal nCode
As
Integer,
ByVal wParam
As
Integer,
ByRef lparam
As MSLLHOOKSTRUCT)
As
Integer
If (nCode = HC_ACTION)
ThennTickIdle = Environment.TickCount
End
If
Return CallNextHookExSouris(hHookSouris, nCode, wParam, lparam)
End
Function#
End
RegionEnd
Module