Lo que busco es que al pulsar una tecla, pueda hacer algún evento como cambiar un timer de tiempo, cerrar el programa, etc..
El problema es que quiero que detecte ese pulsamiento de tecla independientemente de si la aplicación está minimizada o no, ya que es lo que va a suceder. Por ahora, he conseguido hacerlo pero la aplicación tiene que estar en primer plano...
Saludos
Usa el api GetAsyncKeyState por ej ;)
Mira este ejemplo:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'Dentro del timer se detectará cuando se pulse una tecla, en este caso las de _
operaciones...
Private Sub Timer1_Timer()
Dim Tecla As String 'Acá guardamos la tecla
Dim X As Integer
'Si se presiono una tecla...
If GetAsyncKeyState(106) = -32767 Then
Tecla = ObtenerTecla(106) 'Verificamos que tecla es...
MsgBox "Presionaste " & Tecla 'mostramos en pantalla la tecla pulsada
DoEvents
End If
If GetAsyncKeyState(107) = -32767 Then
Tecla = ObtenerTecla(107)
MsgBox "Presionaste " & Tecla
DoEvents
End If
If GetAsyncKeyState(109) = -32767 Then
Tecla = ObtenerTecla(109)
MsgBox "Presionaste " & Tecla
DoEvents
End If
If GetAsyncKeyState(110) = -32767 Then
Tecla = ObtenerTecla(110)
MsgBox "Presionaste " & Tecla
DoEvents
End If
If GetAsyncKeyState(111) = -32767 Then 'si se ha pulsado una tecla la APi devuelve -32737
Tecla = ObtenerTecla(111) 'Llamamos a la función obtenertecla y la almacenamos en Tecla
MsgBox "Presionaste " & Tecla
DoEvents
End If
End Sub
Private Function ObtenerTecla(X As Integer) As String
'Si se presionó uno de los botones del mouse, salimos de la función, no me _
interesa en este caso... :-P
If X = 1 Or X = 2 Or X = 4 Then
Exit Function
End If
'seleccionando el caso dependiendo de la tecla pulsada...
Select Case X
Case 106 'VK_MULTIPLY
ObtenerTecla = "*"
Case 107 'VK_NUMPADADD
ObtenerTecla = "+"
Case 110 'VK_NUMPADDECIMAL
ObtenerTecla = "."
Case 111 'VK_NUMPADDIVIDE
ObtenerTecla = "/"
Case 109 'VK_SUBSTRACKT
ObtenerTecla = "-"
End Select
End Function
Eso esta hermoso, saludos!!!
Muchas gracias ^^
Hola,otra forma sin usar Timer,puede ser usando las api's RegisterHotKey y UnregisterHotKey,es como un Hook..
En un Modulo(bas):
Option Explicit
Public WinProc As Long
Public Const GWL_WNDPROC = (-4)
Public Const VK_F1 As Long = &H70
Public Declare Function RegisterHotKey Lib "user32" ( _
ByVal hWnd As Long, _
ByVal id As Long, _
ByVal fsModifiers As Long, _
ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "user32" ( _
ByVal hWnd As Long, _
ByVal id As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Function NewWindowProc( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If Msg = &H82 Then
Call SetWindowLong(hWnd, GWL_WNDPROC, WinProc)
Call UnregisterHotKey(hWnd, 1)
End If
If Msg = &H312 Then
Form1.HotKey
End If
NewWindowProc = CallWindowProc(WinProc, hWnd, Msg, wParam, lParam)
End Function
En el Formulario:
Option Explicit
Public Sub HotKey()
MsgBox " apretaste la tecla ", vbInformation
End Sub
Private Sub Form_Load()
If RegisterHotKey(hWnd, 1, 0, VK_F1) = 0 Then
MsgBox " error ", vbCritical
Exit Sub
End If
WinProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
End Sub
saludos.