Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: GameAndWatch en 6 Enero 2013, 18:44 pm



Título: Captura del teclado
Publicado por: GameAndWatch en 6 Enero 2013, 18:44 pm
¡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.


Título: Re: Captura del teclado
Publicado por: Høl¥ en 6 Enero 2013, 19:04 pm
Hola, dale un vistazo a esto:

http://www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET


Título: Re: Captura del teclado
Publicado por: GameAndWatch en 6 Enero 2013, 19:32 pm
¡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. :-(


Título: Re: Captura del teclado
Publicado por: Eleкtro en 6 Enero 2013, 20:19 pm
Si lo único que quieres es capturar unas cuantas teclas fuera del form puedes crear hotkeys globales:
Código
  1. #Region " GlobalHotkeys Class "
  2.  
  3.    Class Shortcut
  4.        Inherits NativeWindow
  5.        Implements IDisposable
  6.  
  7. #Region " GlobalHotkeys Declarations "
  8.        Protected Declare Function UnregisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Integer) As Boolean
  9.        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
  10.  
  11.        Event Press(ByVal sender As Object, ByVal e As HotKeyEventArgs)
  12.        Protected EventArgs As HotKeyEventArgs, ID As Integer
  13.  
  14.        Enum Modifier As Integer
  15.            None = 0
  16.            Alt = 1
  17.            Ctrl = 2
  18.            Shift = 4
  19.        End Enum
  20.        Class HotKeyEventArgs
  21.            Inherits EventArgs
  22.            Property Modifier As Shortcut.Modifier
  23.            Property Key As Keys
  24.        End Class
  25.        Class RegisteredException
  26.            Inherits Exception
  27.            Protected Const s As String = "Shortcut combination is in use."
  28.            Sub New()
  29.                MyBase.New(s)
  30.            End Sub
  31.        End Class
  32. #End Region
  33.  
  34. #Region " GlobalHotkeys IDisposable "
  35.        Private disposed As Boolean
  36.        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
  37.            If Not disposed Then UnregisterHotKey(Handle, ID)
  38.            disposed = True
  39.        End Sub
  40.        Protected Overrides Sub Finalize()
  41.            Dispose(False)
  42.            MyBase.Finalize()
  43.        End Sub
  44.        Sub Dispose() Implements IDisposable.Dispose
  45.            Dispose(True)
  46.            GC.SuppressFinalize(Me)
  47.        End Sub
  48. #End Region
  49.  
  50. #Region " GlobalHotkeys debugger "
  51.        <DebuggerStepperBoundary()>
  52.        Sub New(ByVal modifier As Modifier, ByVal key As Keys)
  53.            CreateHandle(New CreateParams)
  54.            ID = GetHashCode()
  55.            EventArgs = New HotKeyEventArgs With {.Key = key, .Modifier = modifier}
  56.            If Not RegisterHotKey(Handle, ID, modifier, key) Then Throw New RegisteredException
  57.        End Sub
  58.        Shared Function Create(ByVal modifier As Modifier, ByVal key As Keys) As Shortcut
  59.            Return New Shortcut(modifier, key)
  60.        End Function
  61.  
  62.        Protected Sub New()
  63.        End Sub
  64.        Protected Overrides Sub WndProc(ByRef m As Message)
  65.            Select Case m.Msg
  66.                Case 786
  67.                    RaiseEvent Press(Me, EventArgs)
  68.                Case Else
  69.                    MyBase.WndProc(m)
  70.            End Select
  71.        End Sub
  72. #End Region
  73.  
  74.    End Class
  75.  
  76. #End Region

Ejemplos de uso:

Código
  1.     ' Declare the GlobalHotkey
  2.  
  3.     Dim WithEvents GlobalHotkey_CTRL_E As Shortcut
  4.     Dim WithEvents GlobalHotkey_ALT_E As Shortcut
  5.     Dim WithEvents GlobalHotkey_ALT_SHIFT_E As Shortcut
  6.  
  7.     ' Create the GlobalHotkey into a sub
  8.  
  9.     GlobalHotkey_CTRL_E = Shortcut.Create(Shortcut.Modifier.Ctrl, Keys.E)
  10.     GlobalHotkey_ALT_E = Shortcut.Create(Shortcut.Modifier.Alt, Keys.E)
  11.     GlobalHotkey_ALT_SHIFT_E = Shortcut.Create(Shortcut.Modifier.Alt Or Shortcut.Modifier.Shift, Keys.E)
  12.  
  13.     Private Sub GlobalHotkey_CTRL_E_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles GlobalHotkey_CTRL_E.Press
  14.        MessageBox.Show("Youve pressed [CTRL + E]")
  15.     End Sub
  16.  
  17.     Private Sub GlobalHotkey_ALT_E_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles GlobalHotkey_ALT_E.Press
  18.        MessageBox.Show("Youve pressed [ALT + E]")
  19.     End Sub
  20.  
  21.     Private Sub GlobalHotkey_ALT_SHIFT_E_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles GlobalHotkey_ALT_SHIFT_E.Press
  22.        MessageBox.Show("Youve pressed [ALT +SHIFT + E]")
  23.     End Sub

Aquí te peudes descargar el snippet:

[APORTE] Snippets (ACTUALIZADO 21/12/2012) (http://foro.elhacker.net/net/aporte_snippets_actualizado_21122012-t378770.0.html)


Título: Re: Captura del teclado
Publicado por: Høl¥ en 6 Enero 2013, 20:58 pm
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


Título: Re: Captura del teclado
Publicado por: GameAndWatch en 6 Enero 2013, 20:59 pm
¡Muchas gracias a los dos! ;D ;-) ;D
En cuanto pueda probarlo (que ahora estoy en ordenador ajeno) le echaré un ojo.