Recognizing keypresses 
Author Message
 Recognizing keypresses

Could someone please tell me how to recognize a keypress
from a userform?

For example, I need to cancel the userform when the escape
key is pressed.

Thanks in advance,
=Ray=



Mon, 28 Feb 2005 23:58:53 GMT  
 Recognizing keypresses
you need a button with code like this

Private Sub closeform_Click()
    Unload Me
End Sub

and in the properties of the button set cancel to true

Regards Ron



Quote:
> Could someone please tell me how to recognize a keypress
> from a userform?

> For example, I need to cancel the userform when the escape
> key is pressed.

> Thanks in advance,
> =Ray=



Tue, 01 Mar 2005 00:30:54 GMT  
 Recognizing keypresses
Thanks, and that helps for that particular example,
however, I would still like to know how to reference
keypresses.

Thanks again,
=Ray=

Quote:
>-----Original Message-----
>you need a button with code like this

>Private Sub closeform_Click()
>    Unload Me
>End Sub

>and in the properties of the button set cancel to true

>Regards Ron



>> Could someone please tell me how to recognize a keypress
>> from a userform?

>> For example, I need to cancel the userform when the
escape
>> key is pressed.

>> Thanks in advance,
>> =Ray=

>.



Tue, 01 Mar 2005 00:45:17 GMT  
 Recognizing keypresses
posted by Rob Bovey

 Unfortunately, there isn't any simple way to do this with UserForms. The
problem is that the UserForm object doesn't have a KeyPreview property like
VB Forms do, so only the currently active control responds to the Key*
events. This means there's no way to get a form-wide preview of what key a
user has pressed at redirect execution if you see one of your shortcut keys.

    The best workaround (and I'd hardly call it good) is to give every
control on the UserForm a KeyDown event that would look for any shortcut
keys and redirect them to the appropriate control. Because there's no way to
cancel the KeyDown event, you'd have to limit yourself to control keys like
the F keys:

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
    ByVal Shift As Integer)
    Select Case KeyCode
        Case 113    ''' F2
            CommandButton1_Click
        Case 114    ''' F3
            CommandButton2_Click
        Case 115    ''' F4
            '''''
    End Select
End Sub



Quote:
> Thanks, and that helps for that particular example,
> however, I would still like to know how to reference
> keypresses.

> Thanks again,
> =Ray=

> >-----Original Message-----
> >you need a button with code like this

> >Private Sub closeform_Click()
> >    Unload Me
> >End Sub

> >and in the properties of the button set cancel to true

> >Regards Ron



> >> Could someone please tell me how to recognize a keypress
> >> from a userform?

> >> For example, I need to cancel the userform when the
> escape
> >> key is pressed.

> >> Thanks in advance,
> >> =Ray=

> >.



Tue, 01 Mar 2005 00:52:23 GMT  
 Recognizing keypresses
I don't know if this will help in a user form, but you can test an
individual key using an API call:

Perhaps you could put this in a timed loop to test the keys you are
interested in?

Stephen Fitzgerald

Option Explicit
'stuff from WINAPI
'GetAsyncKeyState(Key As Long) As Integer
Declare Function GetAsyncKeyState Lib "user32" _
    (ByVal vKey As Long) As Integer

Function IsKeyDown(Key As Long) as Boolean

    Dim temp as Integer

    if GetAsyncKeyState(Key)=0 then
        IsKeyDown=False
    else
        IsKeyDown=True
    end if

End Function

'I put the key code definitions in another module...
'Key codes are:

' Virtual Keys, Standard Set
Public Const VK_LBUTTON = &H1
Public Const VK_RBUTTON = &H2
Public Const VK_CANCEL = &H3
Public Const VK_MBUTTON = &H4             '  NOT contiguous with L RBUTTON

Public Const VK_BACK = &H8
Public Const VK_TAB = &H9

Public Const VK_CLEAR = &HC
Public Const VK_RETURN = &HD

Public Const VK_SHIFT = &H10
Public Const VK_CONTROL = &H11
Public Const VK_MENU = &H12
Public Const VK_PAUSE = &H13
Public Const VK_CAPITAL = &H14

Public Const VK_ESCAPE = &H1B

Public Const VK_SPACE = &H20
Public Const VK_PRIOR = &H21
Public Const VK_NEXT = &H22
Public Const VK_END = &H23
Public Const VK_HOME = &H24
Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_RIGHT = &H27
Public Const VK_DOWN = &H28
Public Const VK_SELECT = &H29
Public Const VK_PRINT = &H2A
Public Const VK_EXECUTE = &H2B
Public Const VK_SNAPSHOT = &H2C
Public Const VK_INSERT = &H2D
Public Const VK_DELETE = &H2E
Public Const VK_HELP = &H2F

' VK_A thru VK_Z are the same as their ASCII equivalents: 'A' thru 'Z'
' VK_0 thru VK_9 are the same as their ASCII equivalents: '0' thru '9'

Public Const VK_NUMPAD0 = &H60
Public Const VK_NUMPAD1 = &H61
Public Const VK_NUMPAD2 = &H62
Public Const VK_NUMPAD3 = &H63
Public Const VK_NUMPAD4 = &H64
Public Const VK_NUMPAD5 = &H65
Public Const VK_NUMPAD6 = &H66
Public Const VK_NUMPAD7 = &H67
Public Const VK_NUMPAD8 = &H68
Public Const VK_NUMPAD9 = &H69
Public Const VK_MULTIPLY = &H6A
Public Const VK_ADD = &H6B
Public Const VK_SEPARATOR = &H6C
Public Const VK_SUBTRACT = &H6D
Public Const VK_DECIMAL = &H6E
Public Const VK_DIVIDE = &H6F
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79
Public Const VK_F11 = &H7A
Public Const VK_F12 = &H7B
Public Const VK_F13 = &H7C
Public Const VK_F14 = &H7D
Public Const VK_F15 = &H7E
Public Const VK_F16 = &H7F
Public Const VK_F17 = &H80
Public Const VK_F18 = &H81
Public Const VK_F19 = &H82
Public Const VK_F20 = &H83
Public Const VK_F21 = &H84
Public Const VK_F22 = &H85
Public Const VK_F23 = &H86
Public Const VK_F24 = &H87

Public Const VK_NUMLOCK = &H90
Public Const VK_SCROLL = &H91

'
'   VK_L VK_R - left and right Alt, Ctrl and Shift virtual keys.
'   Used only as parameters to GetAsyncKeyState() and GetKeyState().
'   No other API or message will distinguish left and right keys in this
way.
'  /
Public Const VK_LSHIFT = &HA0
Public Const VK_RSHIFT = &HA1
Public Const VK_LCONTROL = &HA2
Public Const VK_RCONTROL = &HA3
Public Const VK_LMENU = &HA4
Public Const VK_RMENU = &HA5

Public Const VK_ATTN = &HF6
Public Const VK_CRSEL = &HF7
Public Const VK_EXSEL = &HF8
Public Const VK_EREOF = &HF9
Public Const VK_PLAY = &HFA
Public Const VK_ZOOM = &HFB
Public Const VK_NONAME = &HFC
Public Const VK_PA1 = &HFD
Public Const VK_OEM_CLEAR = &HFE



Quote:
> posted by Rob Bovey

>  Unfortunately, there isn't any simple way to do this with UserForms. The
> problem is that the UserForm object doesn't have a KeyPreview property
like
> VB Forms do, so only the currently active control responds to the Key*
> events. This means there's no way to get a form-wide preview of what key a
> user has pressed at redirect execution if you see one of your shortcut
keys.

>     The best workaround (and I'd hardly call it good) is to give every
> control on the UserForm a KeyDown event that would look for any shortcut
> keys and redirect them to the appropriate control. Because there's no way
to
> cancel the KeyDown event, you'd have to limit yourself to control keys
like
> the F keys:

> Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
>     ByVal Shift As Integer)
>     Select Case KeyCode
>         Case 113    ''' F2
>             CommandButton1_Click
>         Case 114    ''' F3
>             CommandButton2_Click
>         Case 115    ''' F4
>             '''''
>     End Select
> End Sub



> > Thanks, and that helps for that particular example,
> > however, I would still like to know how to reference
> > keypresses.

> > Thanks again,
> > =Ray=

> > >-----Original Message-----
> > >you need a button with code like this

> > >Private Sub closeform_Click()
> > >    Unload Me
> > >End Sub

> > >and in the properties of the button set cancel to true

> > >Regards Ron



> > >> Could someone please tell me how to recognize a keypress
> > >> from a userform?

> > >> For example, I need to cancel the userform when the
> > escape
> > >> key is pressed.

> > >> Thanks in advance,
> > >> =Ray=

> > >.



Sat, 05 Mar 2005 21:26:58 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. In vbs exist the keypress event

2. Keypress event

3. Disabling shift keypress that prevents code execution

4. Keypress Counter

5. Capture keypress

6. Exit Loop on keypress?

7. Keypress

8. Help with Keypress

9. Can VBA intercept keypress within Excel ?

10. Dialog Button Keypress


 
Powered by phpBB® Forum Software © phpBB Group