VBS calls Windows API functions (PostMessage) to implement background send key script

Source: Internet
Author: User

' ==========================================================================

' VBScript Source File--Created with SAPIEN Technologies primalscript 4.0

' NAME:

' Author:microsoft, Microsoft
' DATE:2014/8/10

' COMMENT:

' =================== define variables, register API objects ==================================
Dim Userwrap,hwnd

Set Userwrap = CreateObject ("Dynamicwrapper")
Set ws=wscript.createobject ("Wscript.Shell")

Wscript.Sleep 500

Ws. Run "Calc", 0

Wscript.Sleep 500

' Declare Function showwindow Lib ' user32 ' Alias ' ShowWindow ' (ByVal hwnd as Long, ByVal nCmdShow as long) as long
Userwrap.register "USER32. DLL "," ShowWindow "," I=hl "," F=s "," r=l "

' Declare Function FindWindow Lib ' user32 ' Alias ' Findwindowa ' (ByVal lpclassname As String, ByVal Lpwindowname as String) As Long

Userwrap.register "USER32. DLL "," FindWindow "," I=ss "," F=s "," r=l "

' Declare Function setwindowpos Lib ' user32 ' Alias ' SetWindowPos ' (ByVal hwnd as Long, ByVal hwndinsertafter as Long, ByVal X as long, ByVal y as Long, ByVal CX as Long, ByVal Cy as Long, ByVal wflags as long) as long

Userwrap.register "USER32. DLL "," SetWindowPos "," i=hllllll "," F=s "," r=l "

' Declare Function PostMessage Lib ' user32 ' Alias ' Postmessagea ' (ByVal hwnd as Long, ByVal wmsg as Long, ByVal WParam as L Ong, LParam as any)
Userwrap.register "USER32. DLL "," PostMessage "," I=hlls "," F=s "," r=l "

' Declare Function setwindowtext Lib ' user32 ' Alias ' setwindowtexta ' (ByVal hwnd as Long, ByVal lpstring as String) as Long
Userwrap.register "USER32. DLL "," SetWindowText "," I=hs "," F=s "," r=l "

' Declare Function FindWindowEx Lib ' user32 ' Alias ' findwindowexa ' (ByVal hWnd1 as Long, ByVal HWnd2 as Long, ByVal lpsz1 A s string, ByVal lpsz2 as String) as Long
Userwrap.register "USER32. DLL "," FindWindowEx "," I=llss "," F=s "," r=l "

' Declare Function setcursorpos Lib ' user32 ' (ByVal X as Long, ByVal Y as long) as long

Userwrap.register "USER32. DLL "," Setcursorpos "," I=ll "," F=s "," r=l "

' =================== find control or form handle ===============================


HWnd = Userwrap.findwindow (vbNullString, "calculator")
' HWnd = Userwrap.findwindow ("kugou_ui", vbNullString)

HWnd1 = Userwrap.findwindowex (Hwnd,0,vbnullstring,edit)

' Userwrap.showwindow hwnd,sw_hide

' Userwrap.setwindowtext hWnd, ' Hello World '

' MsgBox hWnd
' MsgBox HWnd1

' Userwrap.setwindowpos hWnd,-1, 0, 0, 0, 0, 3

' MsgBox ' Move the mouse to the top left corner "


' Userwrap.setcursorpos 0,0


' ================= defines system constants ===========================

Private Const wm_keydown = &h100
Private Const wm_keyup= &h101
Private Const WM_CHAR = &h102
Public Const Wm_syskeydown = &h104
Public Const Wm_syskeyup = &h105

' ================= send F1 button =====================
Userwrap.postmessage hWnd, WM_KEYDOWN, 112, 0

' ================= send 1002========================
Userwrap.postmessage Hwnd,wm_keydown, 97,0
Userwrap.postmessage Hwnd,wm_keydown, 96,0
Userwrap.postmessage Hwnd,wm_keydown, 96,0
Userwrap.postmessage Hwnd,wm_keydown, 98,0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.