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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using KeyDown on a whole form

Status
Not open for further replies.

BlueScreenOD

Programmer
Nov 18, 2005
4
US
I'm trying to make it so when you hit "Ctrl+A" it opens up a another form. I figured I could just use

Private Sub UserForm_KeyDown...

But it doesn't work. I tried doing the same thing but on a particular text field and I managed to get that to work, but to apply the same thing on every text filed seems a little messy.

Is there anyway I could set my form up so that if a keydown event occurs on any control a certain piece of code is trigerd?
 
I think they added the KeyPreview property in later versions of Excel (2002/2003?), for those of us using eatlier versions (including me) you can try this MrExcel.com

Hope this helps,
CMP

Instant programmer, just add coffee.
 
The .OnKey for the Application object should work for you.

Sub ClearOnKeyInterrupts()
With Application
'.OnKey "{END}", ""
.OnKey "{HOME}", ""
.OnKey "^{a}", ""
'.OnKey "^{q}", ""
End With
End Sub
Sub SetUpOnKeyInterrupts()
With Application
'.OnKey "{END}", "EndKey"
.OnKey "{HOME}", "HomeKey"
.OnKey "^{a}", "Cntrla"
'.OnKey "^{q}", "Cntrlq"
End With
End Sub

Function HomeKey()
On Local Error GoTo HomeKeyErr:
MsgBox "I hit Home Key"
Exit Function
HomeKeyErr:
End Function

Function Cntrla()
On Local Error GoTo CntrlAErr:
MsgBox "I hit Control a Key"
Exit Function
CntrlAErr:
End Function
 
vbap,

Unfortunately, the OnKey method doesn't fire when a Userform has the focus. I tried this in Excel and if you display the Userform as a modeless form, then click a worksheet, the OnKey method fires; otherwise, not.

CautionMP,

I'm running Excel 2003 and no joy regarding a KeyPreview property (or equivalent).


Regards,
Mike
 
I tried this with a form and it worked. Not sure if its good way to code. When I looked at help it suggests using the Keydown or Keyup ...which I tried. Keydown was triggered when I pressed the Control key (holding it down) and then again when I pressed the 'a' key. With a variable global to the module, I was able to make it work when the it triggered on 2nd key press. However, just using the KeyPress seem to work just fine. I've not code much with forms but hope it works for you.


Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 1 Then 'Control a
Load UserForm2
UserForm2.Show
End If
End Sub
 
The problem with using KeyPressed events (or KeyUp/KeyDown) is that they are triggered only when that particular element has the focus. So, if you have something like
Code:
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 1 Then    'Control a
      UserForm2.Show
    End If
End Sub
and a TextBox control on the Userform has the focus, the UserForm_KeyPressed event doesn't fire but rather the TextBox_KeyPressed event. This was BlueScreenOD's point; i.e. he could make this work, but only by coding this event for every control on the form. The Walkenbach example shows how to set up a single event procedure using a custom class.

Regards,
Mike
 
Ahhh. I'll have to lookup Walkenbach's example of the custom class. I've set up custom event class on the application object for workbooks but not seen an example for a Form.
 
Thx for the responses....

I've been thinking about my probloem and was wondering if it's possible to create a new control called superTextBox (or something spiffy like that). That way I could override the KeyDown method and have it pass values to it's containing form.

Is this even possible in VB? I've never really delved to deep into VB before, but I know it would be a relativly easy task in a language like Java. I'm currently using VB v6.3.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top