PDA

View Full Version : Form-level key handler



malik641
02-22-2007, 11:21 AM
Hey guys,

In VB, a userform has a property called KeyPreview. It's used to allow a form-level key handler to monitor any keys pressed in any control on the form. I don't see any property like this in VBA forms. Does it have a different name? Or was it left out in VBA?

TIA

mvidas
02-22-2007, 01:55 PM
Hey Joseph,

Yes, it was left out of VBA. However you can duplicate it's functionality. I'm attaching a workbook, to use this with your project copy the class module and standard module to your project, then put the code I have in the userform into your _Initialize and _Terminate events of your form. Right now I have the key and keypress'ed control put into the application.statusbar just to show you how it can work. Change the "KeyPressHandler" sub inside the class as needed (or move that sub to a module or something). Hopefully this will get you started! Feel free to ask questions as needed
Matt

mvidas
02-23-2007, 06:55 AM
Hey again,

Strangely, this morning I randomly thought of a couple mistakes I made with the code in the file above. So you'll need to make the following changes to get it to work fully.

Change the AddKeyPressEvents function:Function AddKeyPressEvents(ByRef vObject As Object)
Dim xKeyPress As clsKeyPress, xControl As Object
Set xKeyPress = New clsKeyPress
xKeyPress.AddKeypressEvent vObject
KeyPressCollection.Add xKeyPress '*** ADDED
On Error Resume Next '*** ADDED
For Each xControl In vObject.Controls
' Set xKeyPress = New clsKeyPress '*** REMOVED
' xKeyPress.AddKeypressEvent xControl '*** REMOVED
' KeyPressCollection.Add xKeyPress '*** REMOVED
AddKeyPressEvents xControl '*** ADDED
Next
On Error GoTo 0 '*** ADDED
Set xControl = Nothing
End Function
And the AddKeyPressEvent function in the class:Public Function AddKeypressEvent(ByRef AnObject As Object) As Boolean
Select Case TypeName(AnObject)
Case "CheckBox": Set kpCheckBox = AnObject
Case "ComboBox": Set kpComboBox = AnObject
Case "CommandButton": Set kpCommandButton = AnObject
Case "Frame": Set kpFrame = AnObject
Case "ListBox": Set kpListBox = AnObject
Case "MultiPage": Set kpMultiPage = AnObject
Case "OptionButton": Set kpOptionButton = AnObject
Case "ScrollBar": Set kpScrollBar = AnObject
Case "SpinButton": Set kpSpinButton = AnObject
Case "TabStrip": Set kpTabStrip = AnObject
Case "TextBox": Set kpTextBox = AnObject
Case "ToggleButton": Set kpToggleButton = AnObject
' Case "UserForm": Set kpUserForm = AnObject '***REMOVED
'*** ADDED NEXT BLOCK
Case Else
On Error Resume Next
If TypeName(AnObject) = AnObject.Name Then Set kpUserForm = AnObject
On Error GoTo 0
End Select
End Function That class module function change was unexpected, but I found out the typename of a userform is the userform's name... not sure why..?

Matt

malik641
02-23-2007, 10:19 AM
Hey Matt,

Thanks for the example! I'll be checking it out tonight :) I'll let you know how it goes (also curious about the userform typename thing, I'll do some digging and post back).