Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

F10 Key and KeyDown event

Status
Not open for further replies.

dlpastel

Programmer
Aug 8, 2002
114
US
I want to use the F10 key as a key to save some data so I intended to use the KeyDown event to trigger this. I have found that for some reason however after I press the F10 key no other keys seem to trigger the KeyDown event until I click on the form. All other key combinations work ie: F1 then INS or F1 then F2, etc. As soon as I press F10 it will not work anymore. I included the basic code which is being used on just a blank form with no controls on it. I don't see how it could be focus as there is only one form with no controls on it.


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim i As Integer
Debug.Print "KeyCode"; KeyCode
KeyPreview = True
Select Case KeyCode
Case vbKeyInsert
Case vbKeyF1
Case vbKeyF10
Case vbKeyF12
Case Else
Debug.Print "Key Code was "; KeyCode
End Select
End Sub

Thanks,
Dan
 
And if you press F10 again, the other keys will start working.

I think this is something to do with the VB IDE. If you compile the program and run it, it will work fine.

As a note, your line:

Keypreview = True

Does not set the form's Keypreview property. It's actually creating a boolean variable and setting it to True. ( probabaly because you don't have Option Explicit set in your program, which you really should do ).

Anyway, that should be:

Form.Keypreview = True


Robert
 
It still does not work even as an exe. I even made the code easier with just a form and one label on the form and the
following code in the KeyDown event:

label1.caption = keycode


You are right that after I press F10, I have no control of any other keys until I press F10 again. Then I have control until I press F10

Any other ideas?

Thanks,
Dan
 
I tried it as an exe and it still does not work. I even simplified it with just a form and a label and the following code:

label1.caption = keycode

The label displays each keystroke until I press F10. It then displays the F10 keycode but pressing any other function keys does not work until I press F10 a second time or click my mouse on the form then it works until I press F10 again.

Any ideas?

Thanks,
Dan
 
You got me. I tried it in the IDE and got the symptom you described, then I compiled it into a EXE and then it worked OK.

I'll try to look at it again when I have time.

RT
 
I believe that F10 works as a menu activation key (like ALT)

Under Win2K the menu hotkey underlines are not shown until the ALT (menu activation) key is pressed. The same applies to F10, so the first strike at F10 enters Menu mode, the second (or any other key not activating a menu hotkey) will release the F10.

You may be able to hook the keyboard to get round this, but it may be easier to use another function key
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Set the Form KeyPreview Property to true outside the Keydown event, either in design mode or in the Form_Load Event. The Form does not get to invoke the keydown event before the object on the form with the focus does. The reason it works on the second press of the F10 key is the line KeyPreview = True that is called on the first keydown (which happens after the fact) and sets it from that point forward to fire then Form_Keydown before the other object events in the Form.


Mark
 
Which is probably why it worked for me as a compiled program, because I set the keypreview in design time.

Robert
 
Okay, I started a new project with just a form and a label and here is the code:

Option Explicit

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Label1.Caption = KeyCode
End Sub

Private Sub Form_Load()
Form1.KeyPreview = True
End Sub

It still does not work! As soon as I press F10 it locks up
until I press F10 again. I even set the Keypreview property of the form to true also.

Any other ideas?

Thanks,
Dan
 
As johnwm stated above, the problem you are having is due to the fact that the "F10" key is a system key like "Alt". You will need to get into a "hook" if you wish to use the "F10" key. It would be easier to choose a different function key if possible. If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Did you try setting the Form's KeyPreview property to true while in Design mode? I use F10 in a few of my apps with no problem. But I do not set KeyPreview in code--only in design mode.

Mark
 
I think we need to check on OS - F10 or ALT for menu activation started (I believe) with W2K. Before that menu hotkeys were always underlined and the activation issue didn't apply
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks for all the help guys. I am just going to use Ctrl+s for a save key. I think you are right about it being a sort of reserved key.

Thanks,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top