-
Feb 16th, 2018, 10:58 AM
#1
Thread Starter
New Member
Keyboard Activates Command Button On Form
I have a small application in which I did not have enough commands to put in a menu, so I created buttons on the form. The problem with this is that a key press will trigger the focused button. How do I prevent this?
-
Feb 16th, 2018, 11:05 AM
#2
Re: Keyboard Activates Command Button On Form
I assume you're talking about the "Enter" key, and maybe the "Esc" key.
The way I'd do it is to turn "KeyPreview" on for the form, and "trap" the "Enter" (and maybe "Esc") key so that it didn't mess me up.
Good Luck,
Elroy
EDIT1: Hmmm, interestingly, neither KeyDown nor KeyPress traps the enter key. It seems that it may take a bit of subclassing to get this done.
Last edited by Elroy; Feb 16th, 2018 at 11:10 AM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 16th, 2018, 11:18 AM
#3
Re: Keyboard Activates Command Button On Form
Here, this'll do it:
Code:
Option Explicit
'
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
'
Private Sub Command1_Click()
If GetKeyState(&HD&) < 0 Then Exit Sub
' Do your stuff, it was clicked, not Enter key.
End Sub
You'll have to put that check under each button though (or at least each button that you don't want to respond to the "Enter" key if/when it has the focus).
Also, no subclassing needed.
Good Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 16th, 2018, 11:58 AM
#4
Re: Keyboard Activates Command Button On Form
Originally Posted by ndwaynep
I have a small application in which I did not have enough commands to put in a menu, so I created buttons on the form. The problem with this is that a key press will trigger the focused button. How do I prevent this?
That's what expected on Windows: if you press the Enter key or the Space bar the button on focus executes the click.
-
Feb 16th, 2018, 10:24 PM
#5
Thread Starter
New Member
Re: Keyboard Activates Command Button On Form
Thank you for your replies! I am not looking for a specific key. I want to intercept any key, Below is basically what I have:
There are three command buttons on the form. In the code they are named: cmdStart, cmdStop, and cmdExit.
Code:
Option Explicit
Dim stopPrg as Boolean
Private Sub Form_Load()
stopPrg = False
End Sub
Private Sub cmdStop_Click()
stopPrg = True
End Sub
Private Sub cmdStart_Click()
DoWhatINeed
End Sub
Private Sub cmdExit_Click()
Unload Me
End
End Sub
Private Sub DoWhatINeed
Do
.
.
Loop While stopPrg = False
Exit Sub
When this is running, the "Stop" button on the form has the focus. I want to prevent "any" key press from triggering the stop event.
I hope this will clear the water a bit.
-
Feb 16th, 2018, 11:07 PM
#6
Re: Keyboard Activates Command Button On Form
Originally Posted by ndwaynep
When this is running, the "Stop" button on the form has the focus. I want to prevent "any" key press from triggering the stop event.
I hope this will clear the water a bit.
Then when it is running set the focus to another control, for example:
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
|