Results 1 to 7 of 7

Thread: Capture Enter Key

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    93
    Can you capture the event of the enter key being pressed?

  2. #2
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Talking There

    Put this in the KeyPress Event of a control:

    If KeyAscii = 13 Then
    'Anything you want here
    End If

    Amon Ra
    The Power of Learning.

  3. #3
    Megatron
    Guest
    If you want to capture it throughout the whole App, you need to set the KeyPreview to True, and place the above code in the Form's KeyDown (or KeyPress) event.

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Also, use the vb constant, vbKeyReturn, it helps with readability.

  5. #5
    Matthew Gates
    Guest
    If you want to capture the Enter key being pressed anywhere, even outside your app, use the GetAsyncKeyState API function.


    Code:
    Private Declare Function GetAsyncKeyState _
    Lib "user32" (ByVal vKey As Long) As Integer
    
    
    Private Sub Timer1_Timer()    
        If GetAsyncKeyState(vbKeyReturn) Then Debug.Print "Enter key pressed"
    End Sub

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Matthew, I think using Hook is better choice

    Code:
    '//Put this code under a Form
    Option Explicit
    
    Private Sub Form_Load()
        '//Create the Hook
        hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        '//Remove the Hook
        UnhookWindowsHookEx hHook
    End Sub
    
    '//Put this code under a Basic Module
    Option Explicit
    
    Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Public hHook As Long
    
    Public Const WH_KEYBOARD = 2
    Public Const VK_RETURN = &HD
    
    Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If idHook < 0 Then
            KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
        Else
            If (GetKeyState(VK_RETURN) And &HF0000000) Then
                MsgBox "Enter Key is press."
            End If
            KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
        End If
    End Function
    
    
    'Code improved by vBulletin Tool (Save as...)

  7. #7
    Megatron
    Guest
    That will only work locally. If you want it to work globally, you need to place your code in a standard DLL.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width