-
Ok, this one is the last question for today! ;-)
I would like the enter key to call a sub ("uguale"). I put
the following code in the Form_KeyPress Sub:
If KeyAscii = 13 Then
uguale_Click
KeyAscii = 0
End If
The problem is that if i click a button of the form and then
i press Enter, it does no more call "uguale", but simulates
a click on the button (that got the focus); i have this
problem only with keyascii 13. Is there a way to make Enter
call always "uguale" also if the focus is on a button or something else, without using a keypress event for each button? There are about 60 buttons... would be a little tedious! ;-)
Thankyou
-
I assume this follows on from your other thread :cool:. There isn't really a way to catch the event for them all, without subclassing. Although I expect Megatron will work something out :).
-
Try the GetAsyncKeyState API. Make a Form with a Timer and set it's Interval to 1.
Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Timer1_Timer()
If GetForegroundWindow = Me.hWnd Then If GetAsyncKeyState(vbKeyReturn) = &H8000 Then Call uguale_Click
End Sub
[Edited by Megatron on 08-06-2000 at 02:53 PM]
-
Aha. Polling the keyboard - didn't think of that. One thought, though. Won't it slow down your program a bit? The odd keypress may be missed at the rather abysmal rate the VB timer moves at.
-
Yes, it can produce some rather unpredictable results, but for the most part, it can capture the EnterKey and that's basically what we are concerned about here.
-
I see your point regarding the speed if the Timer's interval was 500 or so. If you set it to 1 (or even 100), it will capture the keys as if you were typing in a TextBox. This API can make a great Spy application for shared computer's, i.e: if someone is writing a private E-mail, your program can capture the keys that were pressed and log them to a file.
-
Well... i feel really like a newbie! ;-) Wow, that's
fantastic...
Eheh! Thanks a lot Megatron and parksie!
-
Hi!
Ok, i've tried the keyboard polling solution but there
are some problems:
1. Sometimes pressing Enter works well: does not press
the button and calls "uguale"
2. Sometimes it presses the button with focus and then
calls "uguale"
3. Sometimes it presses the button with focus and nothing
else
So i'm getting crazy with that! ;-) Do you imagine another
solution? Is there a way to make buttons not to get focus
state (or maybe, to pass focus state to the textfield as
they're relased?)?
If you have no other ideas, well! I'm gonna change each
button's code... Eheh! Thanks a lot... do not get crazy with
this...
Thanx