Al Presionar una Tecla

Access trae sus propios eventos AlBajar, AlSubir o AlPresionar una Tecla, Pero que pasaria si el form esta Minimizado, Oculto o simplemente no tiene el FOCO (Pues nada, no se dispara el evento)
Para ello debemos recurrir a la Funcion API GetAsyncKeyState y crear un Codigo que me detecte la pulsasion de la tecla
 
***   EL CODIGO  ***
 
Option Compare Database
'***************************************************************
'&                                                            &*
'&                                                            &*
'&                                                            &*
'&                                                            &*
'&               Jefferson Jimenez (JJJT)                     &*
'&                 Cabimas - Venezuela                        &*
'&                    Julio - 2010                            &*
'&                                                            &*
'&                                                            &*
'&                                                            &*
'&                                                            &*
'&                                                            &*
'***************************************************************
Option Explicit
'Declaramos al API que me servira para detectar la tecla pulsada
Private Declare Function GetAsyncKeyState _
                                    Lib "user32" ( _
                                    ByVal vKey As Long) As Integer
'Declaramos al API para crear un Timer
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, _
                                    ByVal nIDEvent As Long, _
                                    ByVal uElapse As Long, _
                                    ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, _
                                    ByVal nIDEvent As Long) As Long
'Declaro la variable del FORM
Dim frmForm As Form
'Aca creo el codigo que me servira como detector
Public Sub AlPresionarTecla(frm As Form)
Set frmForm = frm 'Cargo el Form
 SetTimer frm.hwnd, 0, 1, AddressOf DetectaTeclaJJJT 'Abro un Timer
End Sub
Private Sub DetectaTeclaJJJT( _
                    ByVal hwnd As Long, ByVal nIDEvent As Long, _
                    ByVal uElapse As Long, ByVal lpTimerFunc As Long)
'Solo debes elegir la tecla, le pasas el evento y listo
    Dim i As Long
    'para los 255 códigos Ascii
    For i = 0 To 255
        'si se ha pulsado una tecla la APi devuelve -32737
        If GetAsyncKeyState(i) = -32767 Then
           Select Case i
           'Selecciono la tecla segun su valor
            Case 9
            MsgBox "se presiono la tecla : Tab"
            'KillTimer frmForm.hwnd, 0
            Case 13
            MsgBox "se presiono la tecla : Enter"
            'KillTimer frmForm.hwnd, 0
            Case 20
            MsgBox "se presiono la tecla : Mayuscula"
            'KillTimer frmForm.hwnd, 0
            Case 112
            MsgBox "se presiono la tecla : F1"
            'KillTimer frmForm.hwnd, 0
            Case 113
            MsgBox "se presiono la tecla : F2"
            'KillTimer frmForm.hwnd, 0
            Case 114
            MsgBox "se presiono la tecla : F3"
            'KillTimer frmForm.hwnd, 0
            Case 115
            MsgBox "se presiono la tecla : F4"
            'KillTimer frmForm.hwnd, 0
            Case 116
            MsgBox "se presiono la tecla : F5"
            'KillTimer frmForm.hwnd, 0
            Case 117
            MsgBox "se presiono la tecla : F6"
            'KillTimer frmForm.hwnd, 0
            Case 118
            MsgBox "se presiono la tecla : F7"
            'KillTimer frmForm.hwnd, 0
            Case 119
            MsgBox "se presiono la tecla : F8"
            'KillTimer frmForm.hwnd, 0
            Case 120
            MsgBox "se presiono la tecla : F9"
            'KillTimer frmForm.hwnd, 0
            Case 121
            MsgBox "se presiono la tecla : F10"
            'KillTimer frmForm.hwnd, 0
            Case 122
            MsgBox "se presiono la tecla : F11"
            'KillTimer frmForm.hwnd, 0
            Case 123
            MsgBox "se presiono la tecla : F12"
            'KillTimer frmForm.hwnd, 0
           End Select
        End If
    Next i
End Sub 'Fin del Codigo
'***************************************************************
'Para usarlo en cualquier form, en el evento al cargar
'
'Private Sub Form_Load()
'AlPresionarTecla Me
'End Sub
'
'
'JJJT Jefferson Jose Jimenez Tarre (Venezuela)
 
SelectionFile type iconFile nameDescriptionSizeRevisionTimeUser
Comments