Key Event VK Keys

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,069
I have the following script for a couple of VK keys for keyboard event but need the following keys also, can someone please help.

Code:
Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Private Const VK_NUMLOCK = &H90
Private Const VK_TAB = 9
Private Const VK_ENTER = 13
Private Const VK_DOWN = 31
Private Const VK_UP = 30
Private Const VK_a = 65
Private Const vk_lcontrol = 2
Private Const vk_t = 3
Private Const KEYEVENTF_KEYUP = &H2
Declare Function GetKeyState Lib "user32.dll" ( _
ByVal nVirtKey As Long) As Integer
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)


Code:
Sub test()
  ActivateWindow "Firefox"
 
 
 
 If (GetKeyState(vbKeyTab) = 1) Then
    keybd_event VK_TAB, 1, 0, 0
    keybd_event VK_TAB, 1, KEYEVENTF_KEYUP, 0
    keybd_event VK_TAB, 1, 0, 0
    keybd_event VK_TAB, 1, KEYEVENTF_KEYUP, 0
 
  End If
 
 
 
End Sub

The keys I need are

CTRL 1 (together) i.e. SendKeys ("^{1}")

F3

Enter

script to type something like "Hello there"

Escape

If anyone could help I would appreciate it.
 
Hi Kenneth,
I have just started working with your script and have run into problems.

I have changed the \\ in the script to ' and the ; to , and added extensibility and runtime references, however many areas are showing errors.

The modified script is.

Code:
'Send Ctrl+O to open the FileOpenDlg()
KeyPlusChar(17, "O")    '11h or 17 - vk_control
'Send Shift+Tab
KeyPlusKey(16, 9)   '10h or 16 = vk_Shift, 9 = vk_tab
'Send Down and then Up to set focus to first item in FileOpenDlg()
Key (40) '28h=38, vk_down
Key (38) '26h=40, vk_up
Procedure KeyPlusKey(str1, str2)    'e.g. Use this to send key command key plus a command key. e.g. Shift+Tab
    KeyDown (str1)
    Key (str2)
    KeyUp (str1)
EndProc
Procedure KeyPlusChar(str1, str2)   'e.g. Use this to send key command plus a key combination. e.g. Ctrl+O
    KeyDown (str1)
    Keys (str2)
    KeyUp (str1)
EndProc
Procedure Keys(Str) 'KeyDown() and KeyUp() for each character string in str.
    ForNext(i,1,StrLen(str))
        s = CtoN(SubStr(Str, i, 1))
        Key (s)
    EndFor
EndProc
Procedure KeyUp(Str)    'Release a key
DllCall Prototype keybd_event ("user32.dll", "keybd_event", DWord!, {bVk, bScan, DWord(dwFlags),  DWord(dwExtraInfo)})
keybd_event(str, 9dh, 2, 0)
EndProc
Procedure KeyDown(Str)  'Press a key
DllCall Prototype keybd_event ("user32.dll", "keybd_event", DWord!, {bVk, bScan, DWord(dwFlags),  DWord(dwExtraInfo)})
keybd_event(str, 9dh, 0, 0)
EndProc
Procedure Key(Str)      'Press and release a key (str must be an integer)
    KeyDown (Str)
    KeyUp (Str)
EndProc

If you could get me started with an F5 command and a ctrl+T command I should be able to go from there (once you have advised what is wrong with my modified script)

Thanks
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
The routines for Keys() needs a bit of work. Sending an uppercase letter is returned as lowercase. I like the class method but there are some issues with it as well.

Code:
Option Explicit

'vk_keys, http://msdn.microsoft.com/en-us/library/ms927178.aspx

Declare Sub keybd_event Lib "user32.dll" _
  (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Sub Test_keybd_event1()
  'F5
  Key 116
  
  'Ctrl+T
  'KeyPlusKey 17, Asc("T")
  'or
  KeyPlusChar 17, "T"
End Sub

Sub Test_keybd_event2()
  'Send Ctrl+O
  KeyPlusChar 17, "O"    '11h or 17 - vk_control
  'Send Shift+Tab
  KeyPlusKey 16, 9   '10h or 16 = vk_Shift, 9 = vk_tab
  'Send Down and then Up to set focus to first item in FileOpenDlg()
  Key 40 '&H28 = 38, vk_down
  Key 38 '&H26 = 40, vk_up
End Sub

' Use this to send key command key plus a command key. e.g. Shift+Tab
Sub KeyPlusKey(str1 As Variant, str2 As Variant)
    KeyDown str1
    Key str2
    KeyUp str1
End Sub

' Use this to send key command plus a key combination. e.g. Ctrl+O
Sub KeyPlusChar(str1 As Variant, str2 As Variant)
    KeyDown str1
    Keys str2
    KeyUp str1
End Sub

' KeyDown() and KeyUp() for each character string in str.
Sub Keys(str As Variant)
    Dim i As Integer, s As String, j As Integer
    For i = 1 To Len(str)
        s = Mid(str, i, 1)
        For j = 1 To 330
          'Debug.Print j, Asc(s) - j
        Next j
        If Val(s) = 0 Then s = Asc(s)
        DoEvents
        Key Val(s)
    Next i
End Sub

' Release a key
Sub KeyUp(str As Variant)
  keybd_event str, &H9D, 2, 0
End Sub

' Press a key
Sub KeyDown(str As Variant)
  keybd_event str, &H9D, 0, 0
End Sub

' Press and release a key
Sub Key(str As Variant)
    KeyDown str
    KeyUp str
End Sub
 
Upvote 0
Hi Kenneth,

I have another question on the Vk keys.

If I want to run several scripts at a time, what code should I put in between each process, or do I need to split them into different procedures.

The following is an example of what I am trying to do, however I will be adding other processes once I can get this to work.

First process is control + 2 to go to firefox tab 2
Second process is F5 to refresh
Third Process is Control + 1 to go to firefox tab 1
Fourth Process is to hit enter key


Code:
Sub Test_keybd_event1()
ActivateWindow "firefox"
KeyPlusChar 17, 62 ' CONTROLKEY + TAB2
 
Key 116 'F5 KEY TO REFRESH
KeyPlusChar 17, 61 ' CONTROLKEY + TAB1
 
Key 0d ' ENTER KEY
 
End Sub
 
Upvote 0
I have worked out how to out extra variants.

Thanks again Kenneth.

Code:
Sub KeyPlusChar(str1 As Variant, str2 As Variant, str3 As Variant, str4 As Variant)
    keydown str1
    Keys str2
    KeyUp str1
     keydown str3
    Keys str4
    KeyUp str3
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top