|
-
Apr 7th, 2001, 11:59 AM
#1
Thread Starter
Lively Member
Can you capture the event of the enter key being pressed?
-
Apr 7th, 2001, 12:18 PM
#2
Hyperactive Member
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.
-
Apr 7th, 2001, 12:37 PM
#3
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.
-
Apr 7th, 2001, 01:08 PM
#4
PowerPoster
Also, use the vb constant, vbKeyReturn, it helps with readability.
-
Apr 7th, 2001, 04:08 PM
#5
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
-
Apr 9th, 2001, 08:50 AM
#6
PowerPoster
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...)
-
Apr 9th, 2001, 02:27 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|