Captura del teclado

Iniciado por GameAndWatch, 6 Enero 2013, 18:44 PM

0 Miembros y 2 Visitantes están viendo este tema.

GameAndWatch

¡Hola! :D
Estaba intentando crear una aplicación que al presionar una tecla (por ejemplo INSERt o IMP PANT) se ejecutara una alerta. ;-)
El problema es que al perder el foco el form, pierde los posibles eventos.
¿Cómo puedo hacer que funciones esa captura incluso cuando esté haciendo otra cosa? :huh:

Gracias de antemano.


GameAndWatch

¡Gracias por responder!
¿No sabes de algun sitio donde esté un poco más resumido?
Es que no me entero de nada... :-[

Me he descargado el source, pero me he quedado igual. :-(

Eleкtro

#3
Si lo único que quieres es capturar unas cuantas teclas fuera del form puedes crear hotkeys globales:
Código (vbnet) [Seleccionar]

#Region " GlobalHotkeys Class "

   Class Shortcut
       Inherits NativeWindow
       Implements IDisposable

#Region " GlobalHotkeys Declarations "
       Protected Declare Function UnregisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Integer) As Boolean
       Protected Declare Function RegisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Integer, ByVal modifier As Integer, ByVal vk As Integer) As Boolean

       Event Press(ByVal sender As Object, ByVal e As HotKeyEventArgs)
       Protected EventArgs As HotKeyEventArgs, ID As Integer

       Enum Modifier As Integer
           None = 0
           Alt = 1
           Ctrl = 2
           Shift = 4
       End Enum
       Class HotKeyEventArgs
           Inherits EventArgs
           Property Modifier As Shortcut.Modifier
           Property Key As Keys
       End Class
       Class RegisteredException
           Inherits Exception
           Protected Const s As String = "Shortcut combination is in use."
           Sub New()
               MyBase.New(s)
           End Sub
       End Class
#End Region

#Region " GlobalHotkeys IDisposable "
       Private disposed As Boolean
       Protected Overridable Sub Dispose(ByVal disposing As Boolean)
           If Not disposed Then UnregisterHotKey(Handle, ID)
           disposed = True
       End Sub
       Protected Overrides Sub Finalize()
           Dispose(False)
           MyBase.Finalize()
       End Sub
       Sub Dispose() Implements IDisposable.Dispose
           Dispose(True)
           GC.SuppressFinalize(Me)
       End Sub
#End Region

#Region " GlobalHotkeys debugger "
       <DebuggerStepperBoundary()>
       Sub New(ByVal modifier As Modifier, ByVal key As Keys)
           CreateHandle(New CreateParams)
           ID = GetHashCode()
           EventArgs = New HotKeyEventArgs With {.Key = key, .Modifier = modifier}
           If Not RegisterHotKey(Handle, ID, modifier, key) Then Throw New RegisteredException
       End Sub
       Shared Function Create(ByVal modifier As Modifier, ByVal key As Keys) As Shortcut
           Return New Shortcut(modifier, key)
       End Function

       Protected Sub New()
       End Sub
       Protected Overrides Sub WndProc(ByRef m As Message)
           Select Case m.Msg
               Case 786
                   RaiseEvent Press(Me, EventArgs)
               Case Else
                   MyBase.WndProc(m)
           End Select
       End Sub
#End Region

   End Class

#End Region


Ejemplos de uso:

Código (vbnet) [Seleccionar]
    ' Declare the GlobalHotkey
   
    Dim WithEvents GlobalHotkey_CTRL_E As Shortcut
    Dim WithEvents GlobalHotkey_ALT_E As Shortcut
    Dim WithEvents GlobalHotkey_ALT_SHIFT_E As Shortcut

    ' Create the GlobalHotkey into a sub
   
    GlobalHotkey_CTRL_E = Shortcut.Create(Shortcut.Modifier.Ctrl, Keys.E)
    GlobalHotkey_ALT_E = Shortcut.Create(Shortcut.Modifier.Alt, Keys.E)
    GlobalHotkey_ALT_SHIFT_E = Shortcut.Create(Shortcut.Modifier.Alt Or Shortcut.Modifier.Shift, Keys.E)

    Private Sub GlobalHotkey_CTRL_E_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles GlobalHotkey_CTRL_E.Press
       MessageBox.Show("Youve pressed [CTRL + E]")
    End Sub

    Private Sub GlobalHotkey_ALT_E_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles GlobalHotkey_ALT_E.Press
       MessageBox.Show("Youve pressed [ALT + E]")
    End Sub

    Private Sub GlobalHotkey_ALT_SHIFT_E_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles GlobalHotkey_ALT_SHIFT_E.Press
       MessageBox.Show("Youve pressed [ALT +SHIFT + E]")
    End Sub


Aquí te peudes descargar el snippet:

[APORTE] Snippets (ACTUALIZADO 21/12/2012)








Høl¥

Lo único que tienes que hacer es incluir en tu solución el proyecto ManagedHooks, (añadirlo en references de tu proyecto también) y meter la .dll que te genera SystemHookCore en el directorio donde tengas el .exe.

Te dejo en el link un esquema de como sería.

http://www.mediafire.com/?aryl21n7g28z24f

GameAndWatch

¡Muchas gracias a los dos! ;D ;-) ;D
En cuanto pueda probarlo (que ahora estoy en ordenador ajeno) le echaré un ojo.