Well , I'm having some trouble with a timer :
The meaning of this program is that when I first press down the ctrl button , the captions of label 1 and 2 should come in labels 29 and 30 ,
the second time I press ctrl , it should also come in labels 31 and 32. The third time I press ctrl , the captions should be - .
The form contains : label1 , label 2 , label 29 , label 30 , label31 , label 32 and timer2Code:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Private Sub Form_Load() Timer2.Interval = 1 End Sub Private Sub Timer2_Timer() On Error Resume Next If GetAsyncKeyState(17) Then Key = True End If If Label29.Caption = "-" And Key = True Then Label29.Caption = Label1.Caption Label30.Caption = Label2.Caption Key = False End If If Label29.Caption <> "-" And Label31.Caption = "-" And Key = True Then Label31.Caption = Label1.Caption Label32.Caption = Label2.Caption Key = False End If If Label31.Caption <> "-" And Label29.Caption <> "-" And Key = True Then Label29.Caption = "-" Label30.Caption = "-" Label31.Caption = "-" Label32.Caption = "-" Key = False End If End Sub
Where do you declare key? Also, why do you use a timer for a key press event? You should just capture the key even and then change the labels.
[HIGHLIGHT="VB"]
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyControl then
If Label29.Caption = "-" And Key = True Then
Label29.Caption = Label1.Caption
Label30.Caption = Label2.Caption
Key = False
End If
If Label29.Caption <> "-" And Label31.Caption = "-" And Key = True Then
Label31.Caption = Label1.Caption
Label32.Caption = Label2.Caption
Key = False
End If
If Label31.Caption <> "-" And Label29.Caption <> "-" And Key = True Then
Label29.Caption = "-"
Label30.Caption = "-"
Label31.Caption = "-"
Label32.Caption = "-"
Key = False
End If
End Sub
[/HIGHLIGHT]
First of all : thanks !!!
(second : you forgot an end if )
Could you also make it so that even when the window isn't activated , when you push ctrl , it still does what it's supposed to do ?
Last edited by ikkeugh; 10-15-2007 at 10:37 AM.
Whoops, I did.
Capturing key strokes when the Window isn't active is a bit harder. You will need to use the GetAsyncKeyState to capture keys in a non-active window.
Declare it:
[highlight=vb]
Private Declare Function GetAsyncKeyState Lib "User32" _
(ByVal vKey As KeyCodeConstants) As Long
[/highlight]
Example Usage:
[highlight=vb]
Private Function KeyDown(ByVal vKey As KeyCodeConstants) _
As Boolean
KeyDown = GetAsyncKeyState(vKey) And &H8000
If KeyDown(vbKeyControl) Then
` code Here
end if
End Sub
[/highlight]
That's not really working ...
exemple
Code:Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As KeyCodeConstants) As Long Private Function KeyDown(ByVal vKey As KeyCodeConstants) As Boolean KeyDown = GetAsyncKeyState(vKey) And &H8000 If KeyDown(vbKeyControl) Then Label1.Caption = "-" End If End Sub
Isn't there an API for this ?
For an example : getmouseposition also works when it isn't in an activated window .
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks