Key Event VK Keys

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
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.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Per the forum guidelines, please do not keep bumping your post multiple times a day.
 
Upvote 0
Can you please advise what I should do instead, do I keep posting a new thread, as I am after an answer and not receiving replies as it keeps getting pushed to the bottom and therefore no-one see's it.
Thanks
 
Upvote 0
No!

It means no one has a suggestion for you. There are very few posts -- if any -- that are ignored and simply "pushed down."

Why not check google/bing for "keybd_event" (w/o the quotes)? You might find helpful documentation / examples. ;)

Can you please advise what I should do instead, do I keep posting a new thread, as I am after an answer and not receiving replies as it keeps getting pushed to the bottom and therefore no-one see's it.
Thanks
 
Upvote 0
MSDN is a good place to find vk_keys: http://msdn.microsoft.com/en-us/library/ms927178.aspx

I don't have time to convert this to VBA right now. This code is in PerfectScript. It should not be difficult to see how to do it in VBA.

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
 
Last edited:
Upvote 0
Thanks for the help Kenneth, It's great when people provide you with solutions.

The website you provided is helpful.

I thought that onkeys were like sendkeys and therefore unreliable so I stayed away from them.

I did google Vk keys, but when you are unsure of the correct terminology it is very hard to get the right results.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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