Jump to content

Send virtual key codes


Recommended Posts

Sends 0x41 ("A") key.

DllCall('user32.dll', 'int', 'keybd_event', 'int', 0x41, 'int', 0, 'int', 0, 'ptr', 0)
DllCall('user32.dll', 'int', 'keybd_event', 'int', 0x41, 'int', 0, 'int', 2, 'ptr', 0)
Edited by Yashied
Link to comment
Share on other sites

  • 1 year later...
  • Moderators

socap,

Just wrap those 2 lines in a function: ;)

_Send_Virtual_Key(0x41)

Func _Send_Virtual_Key($iCode)
    If Not IsInt($iCode) Then Return
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $iCode, 'int', 0, 'int', 0, 'ptr', 0)
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $iCode, 'int', 0, 'int', 2, 'ptr', 0)
EndFunc

How much more convenient would you like it to be? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 7 years later...
On 1/27/2012 at 12:26 PM, Melba23 said:

socap,

 

Just wrap those 2 lines in a function: ;)

 

_Send_Virtual_Key(0x41)

Func _Send_Virtual_Key($iCode)
    If Not IsInt($iCode) Then Return
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $iCode, 'int', 0, 'int', 0, 'ptr', 0)
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $iCode, 'int', 0, 'int', 2, 'ptr', 0)
EndFunc

How much more convenient would you like it to be? :)

 

M23

This works fine, but how can I direct to a specific window?

Link to comment
Share on other sites

Depends on the application you are trying to send keys to : ControlSend is one way or _SendMessageA could work too. Otherwise, you will have to resort to put the application in foreground...

Link to comment
Share on other sites

About _SendMessage, I found this perfect example of Oasis375

 

#include <SendMessage.au3>
#include <WindowsConstants.au3>

Run("notepad.exe")
$hWnd = WinWait("[CLASS:Notepad]", "", 10)
$hControl = ControlGetHandle($hWnd, "", "Edit1")
$text = "this is a line"
$struct_string = DllStructCreate("char[" & StringLen($text) + 1 & "]")
DllStructSetData($struct_string, 1, $text)
_SendMessageA($hControl, $WM_SETTEXT, 0, DllStructGetPtr($struct_string))

 

Link to comment
Share on other sites

  • 2 years later...

please tell me how to send a virtual key combination,

for example, Alt+a, "^a", "{LCTRL}a"

  When I send $VK_SELECT (SELECT key) nothing happens

_Send_Virtual_Key(0x29) ; -> VK_SELECT - SELECT key

Func _Send_Virtual_Key($iCode)
    If Not IsInt($iCode) Then Return
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $iCode, 'int', 0, 'int', 0, 'ptr', 0)
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $iCode, 'int', 0, 'int', 2, 'ptr', 0)
EndFunc

 

Edited by Norm73
Link to comment
Share on other sites

virtual key combination (Left CONTROL key + A)

DllCall('user32.dll', 'int', 'keybd_event', 'int', 0xA2, 'int', 0, 'int', 0, 'ptr', 0)  ;~  Left CONTROL key
DllCall('user32.dll', 'int', 'keybd_event', 'int', 0x41, 'int', 0, 'int', 0, 'ptr', 0)  ;~ A
DllCall('user32.dll', 'int', 'keybd_event', 'int', 0x41, 'int', 0, 'int', 2, 'ptr', 0)
DllCall('user32.dll', 'int', 'keybd_event', 'int', 0xA2, 'int', 0, 'int', 2, 'ptr', 0)

 

Link to comment
Share on other sites

53 minutes ago, jugador said:

virtual key combination (Left CONTROL key + A)

Thanks for the quick reply
It works very well!
I want to make this message more reliable, but I don't know how to send a combination to a control in a window.
Like this:

#include <SendMessage.au3>
#include <WindowsConstants.au3>

Run("notepad.exe")
$hWnd = WinWait("[CLASS:Notepad]", "", 10)
$hControl = ControlGetHandle($hWnd, "", "Edit1")
$text = "this is a line"
$struct_string = DllStructCreate("char[" & StringLen($text) + 1 & "]")
DllStructSetData($struct_string, 1, $text)
_SendMessageA($hControl, $WM_SETTEXT, 0, DllStructGetPtr($struct_string))

 

Edited by Norm73
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...