|
-
Oct 8th, 2000, 03:35 AM
#1
Thread Starter
Junior Member
Hello there,
How it would be possible to carry out an event which runs always when some key is pressed, wherever the focus is. But only when the form is activated where the event is...
Hmm... The explanation was little bit confused but I hope that you will figure out what I mean... 
- Ville
......................
Ville Mattila
-
Oct 8th, 2000, 03:42 AM
#2
Set the Form's KeyPreview property to True.
Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 96
MsgBox "~"
End Select
End Sub
This will work no matter which control is selected.
-
Oct 8th, 2000, 05:43 AM
#3
Frenzied Member
To capture all keypresses in the whole OS use
Code:
'In a module
Public Const DT_CENTER = &H1
Public Const DT_WORDBREAK = &H10
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, ByVal lpDrawTextParams As Any) As Long
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Global Cnt As Long, sSave As String, sOld As String, Ret As String
Dim Tel As Long
Function GetPressedKey() As String
For Cnt = 32 To 128
'Get the keystate of a specified key
If GetAsyncKeyState(Cnt) <> 0 Then
GetPressedKey = Chr$(Cnt)
Exit For
End If
Next Cnt
End Function
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Ret = GetPressedKey
If Ret <> sOld Then
sOld = Ret
sSave = sSave + sOld
End If
End Sub
'In a form
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Me.Caption = "Key Spy"
'Create an API-timer
SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
End Sub
Private Sub Form_Paint()
Dim R As RECT
Const mStr = "Start this project, go to another application, type something, switch back to this application and unload the form. If you unload the form, a messagebox with all the typed keys will be shown."
'Clear the form
Me.Cls
'API uses pixels
Me.ScaleMode = vbPixels
'Set the rectangle's values
SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight
'Draw the text on the form
DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0&
End Sub
Private Sub Form_Resize()
Form_Paint
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Kill our API-timer
KillTimer Me.hwnd, 0
'Show all the typed keys
MsgBox sSave
End Sub
This will save everything to sSave
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Oct 8th, 2000, 10:41 AM
#4
A much more simpler way, since I'm always trying to make life easier .
Code:
Private Declare Function GetAsyncKeyState _
Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
Dim i As Integer
For i = 3 To 255
If GetAsyncKeyState(i) Then Debug.Print Chr(i)
Next
End Sub
-
Sep 16th, 2008, 12:35 PM
#5
Hyperactive Member
Re: Global keypress?
A much more simpler way, since I'm always trying to make life easier  .
Code:
Private Declare Function GetAsyncKeyState _
Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
Dim i As Integer
For i = 3 To 255
If GetAsyncKeyState(i) Then Debug.Print Chr(i)
Next
End Sub
OMG OMG OMG!!!!
This is SOO SOLVED
thanks i've been looking for this
who are you???
-
Sep 16th, 2008, 01:27 PM
#6
Hyperactive Member
Re: Global keypress?
Or if you want to be spoonfed, use the keyboard hooks.
And if you would like to use the key presses only, use the Form_KeyPress(KeyAscii As Integer) event and make sure you set the form's KeyPreview property to True.
-
Sep 16th, 2008, 01:31 PM
#7
Hyperactive Member
Re: Global keypress?
Well i wanted to catch keypress outside the form...and i did that with GetAsyncKeyState
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
|