ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Key events on user form (https://www.excelbanter.com/excel-programming/399325-key-events-user-form.html)

Ken Warthen[_2_]

Key events on user form
 
I'm trying to capture the keycodes so that the focus will move to a specific
control based on whether the user hits TAB or ENTER, or SHIFT+TAB after
entering data in a given text box on my form. I've been trying to do so from
the Key_Down event of the text box, but am not having much luck. Any help
will be greatly appreciated.

John Bundy

Key events on user form
 
Modify this to do tab or enter or shift or whatever you want. Originally
posted by Ron DeBruin, tests for Control key being pressed. If you need help
modifying post.

Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer

Const VK_CONTROL As Integer = &H11 'Ctrl


Sub test()
If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
If Ctrl = True Then
MsgBox "pressed"
Else
MsgBox "Not"
End If
End Sub



'And this in the sheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call test
End Sub


--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"Ken Warthen" wrote:

I'm trying to capture the keycodes so that the focus will move to a specific
control based on whether the user hits TAB or ENTER, or SHIFT+TAB after
entering data in a given text box on my form. I've been trying to do so from
the Key_Down event of the text box, but am not having much luck. Any help
will be greatly appreciated.


Ken Warthen[_2_]

Key events on user form
 
John,

The controls and keystrokes I'm trying to manipulate are on a user form, not
a spreadsheet. Does this matter? I thought their were internal VBA commands
that could be used to achieve my goal here without having to call an external
user library.

Ken

"John Bundy" wrote:

Modify this to do tab or enter or shift or whatever you want. Originally
posted by Ron DeBruin, tests for Control key being pressed. If you need help
modifying post.

Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer

Const VK_CONTROL As Integer = &H11 'Ctrl


Sub test()
If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
If Ctrl = True Then
MsgBox "pressed"
Else
MsgBox "Not"
End If
End Sub



'And this in the sheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call test
End Sub


--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"Ken Warthen" wrote:

I'm trying to capture the keycodes so that the focus will move to a specific
control based on whether the user hits TAB or ENTER, or SHIFT+TAB after
entering data in a given text box on my form. I've been trying to do so from
the Key_Down event of the text box, but am not having much luck. Any help
will be greatly appreciated.



All times are GMT +1. The time now is 02:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com