Keydown

venerdì 13 maggio 2011 - 15.29

analog Profilo | Newbie

Ciao, sono nuovo del forum !
Sto usando vb 2010 come linguaggio per un applicazione che mi apra diversi programmi presenti sul mio pc tramite i tasti funzione.Quindi sto usando la funzione shell con gli eventi key down.
Fino a qui tutto bene, il problema è che la form dovrà stare li, bella che ripristinata..
Come faccio quindi a gestire l'evento keydown senza avere un focus ?
Vi ringrazio anticipatamente :D

Brainkiller Profilo | Guru

>Ciao, sono nuovo del forum !
>Sto usando vb 2010 come linguaggio per un applicazione che mi
>apra diversi programmi presenti sul mio pc tramite i tasti funzione.Quindi
>sto usando la funzione shell con gli eventi key down.
>Fino a qui tutto bene, il problema è che la form dovrà stare
>li, bella che ripristinata..
>Come faccio quindi a gestire l'evento keydown senza avere un
>focus ?

Ciao,
non hai certamente scelto una cosa facilissima, anzi. Bisogna avere conoscenza di Win32 API di come funziona Windows, ecc. Per fare ciò che chiedi devi creare un Hook di sistema, cioè un meccanismo che ti consente di processare tasti e click del mouse anche senza che l'applicazione abbia il focus.
Qui trovi una guida su come creare un hook per mouse in VB.NET:
http://support.microsoft.com/kb/319524/it

E qui trovi un ottimo articolo di Dino Esposito:
http://msdn.microsoft.com/en-us/magazine/cc188966.aspx

se non hai buon esperienza di Win32 API sarà difficile comprendere quanto c'è scritto nelle due pagine qui sopra.
Ciao


David De Giacomi | <empty>
http://blogs.dotnethell.it/david/

analog Profilo | Newbie

Bene, non sembra poi così complicato anche se il codice mi è poco chiaro.
Un hook per tastiera invece ?

Brainkiller Profilo | Guru

>Bene, non sembra poi così complicato anche se il codice mi è
>poco chiaro.
>Un hook per tastiera invece ?

Lo vedi nel secondo link che ti ho mandato.
Troverai degli enum dove è possibile specificare il tipo di hook.
Tra gli altri c'è anche quello per tastiera.

David De Giacomi | <empty>
http://blogs.dotnethell.it/david/

analog Profilo | Newbie

Anzi, una volta trovato come lo uso ?
Per il momento ho trovato questo

Imports System.Runtime.InteropServices Imports System.Reflection Imports System.Drawing Imports System.Threading Module Keyboard Public Declare Function UnhookWindowsHookEx Lib "user32" _ (ByVal hHook As Integer) As Integer Public Declare Function SetWindowsHookEx Lib "user32" _ Alias "SetWindowsHookExA" (ByVal idHook As Integer, _ ByVal lpfn As KeyboardHookDelegate, 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 CallNextHookEx Lib "user32" _ (ByVal hHook As Integer, _ ByVal nCode As Integer, _ ByVal wParam As Integer, _ ByVal lParam As KBDLLHOOKSTRUCT) As Integer Public Structure KBDLLHOOKSTRUCT Public vkCode As Integer Public scanCode As Integer Public flags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure ' Low-Level Keyboard Constants Private Const HC_ACTION As Integer = 0 Private Const LLKHF_EXTENDED As Integer = &H1 Private Const LLKHF_INJECTED As Integer = &H10 Private Const LLKHF_ALTDOWN As Integer = &H20 Private Const LLKHF_UP As Integer = &H80 ' Virtual Keys Public Const VK_TAB = &H9 Public Const VK_CONTROL = &H11 Public Const VK_ESCAPE = &H1B Public Const VK_DELETE = &H2E Private Const WH_KEYBOARD_LL As Integer = 13& Public KeyboardHandle As Integer ' Implement this function to block as many ' key combinations as you'd like Public Function IsHooked( _ ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode) Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE) Debug.WriteLine(Hookstruct.vkCode = VK_TAB) If (Hookstruct.vkCode = VK_ESCAPE) And _ CBool(GetAsyncKeyState(VK_CONTROL) _ And &H8000) Then Call HookedState("Ctrl + Esc blocked") Return True End If If (Hookstruct.vkCode = VK_TAB) And _ CBool(Hookstruct.flags And _ LLKHF_ALTDOWN) Then Call HookedState("Alt + Tab blockd") Return True End If If (Hookstruct.vkCode = VK_ESCAPE) And _ CBool(Hookstruct.flags And _ LLKHF_ALTDOWN) Then Call HookedState("Alt + Escape blocked") Return True End If Return False End Function Private Sub HookedState(ByVal Text As String) Debug.WriteLine(Text) End Sub Public Function KeyboardCallback(ByVal Code As Integer, _ ByVal wParam As Integer, _ ByRef lParam As KBDLLHOOKSTRUCT) As Integer If (Code = HC_ACTION) Then Debug.WriteLine("Calling IsHooked") If (IsHooked(lParam)) Then Return 1 End If End If Return CallNextHookEx(KeyboardHandle, _ Code, wParam, lParam) End Function Public Delegate Function KeyboardHookDelegate( _ ByVal Code As Integer, _ ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _ As Integer <MarshalAs(UnmanagedType.FunctionPtr)> _ Private callback As KeyboardHookDelegate Public Sub HookKeyboard() callback = New KeyboardHookDelegate(AddressOf KeyboardCallback) KeyboardHandle = SetWindowsHookEx( _ WH_KEYBOARD_LL, callback, _ Marshal.GetHINSTANCE( _ [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0) Call CheckHooked() End Sub Public Sub CheckHooked() If (Hooked()) Then Debug.WriteLine("Keyboard hooked") Else Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError) End If End Sub Private Function Hooked() Hooked = KeyboardHandle <> 0 End Function Public Sub UnhookKeyboard() If (Hooked()) Then Call UnhookWindowsHookEx(KeyboardHandle) End If End Sub End Module

Brainkiller Profilo | Guru

Ti giro un altro link, perchè il precedente consentiva la creazione di local hooks, invece sono necessari i global hooks.
Il link è il seguente:
http://www.codeproject.com/KB/system/globalsystemhook.aspx
C'è già una library pronta, così fai meno fatica.
Mi raccomando studiati un po' di roba sugli Hooks, cosa sono, a cosa servono, come funzionano, se no avrai difficoltà nel capire e implementare.

Ciao

David De Giacomi | <empty>
http://blogs.dotnethell.it/david/
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5