RE: [aus-dotnet] simulating a keypress


    [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
    • From: ben.issa
    • Subject: RE: [aus-dotnet] simulating a keypress
    • Date: Thu, 03 Mar 2005 10:04:36 +1100

    Jarrod, 
    
    Some extra stuff (note the VK_NUMLOCK and VK_NUMPAD... Constants)
    
    Public Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte,
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    
    Public Const VK_LBUTTON = &H1
    Public Const VK_RBUTTON = &H2
    Public Const VK_CANCEL = &H3
    Public Const VK_MBUTTON = &H4
    Public Const VK_BACK = &H8
    Public Const VK_TAB = &H9
    Public Const VK_CLEAR = &HC
    Public Const VK_RETURN = &HD
    Public Const VK_SHIFT = &H10
    Public Const VK_CONTROL = &H11
    Public Const VK_MENU = &H12
    Public Const VK_PAUSE = &H13
    Public Const VK_CAPITAL = &H14
    Public Const VK_ESCAPE = &H1B
    Public Const VK_SPACE = &H20
    Public Const VK_PRIOR = &H21
    Public Const VK_NEXT = &H22
    Public Const VK_END = &H23
    Public Const VK_HOME = &H24
    Public Const VK_LEFT = &H25
    Public Const VK_UP = &H26
    Public Const VK_RIGHT = &H27
    Public Const VK_DOWN = &H28
    Public Const VK_SELECT = &H29
    Public Const VK_PRINT = &H2A
    Public Const VK_EXECUTE = &H2B
    Public Const VK_SNAPSHOT = &H2C
    Public Const VK_INSERT = &H2D
    Public Const VK_DELETE = &H2E
    Public Const VK_HELP = &H2F
    Public Const VK_0 = &H30
    Public Const VK_1 = &H31
    Public Const VK_2 = &H32
    Public Const VK_3 = &H33
    Public Const VK_4 = &H34
    Public Const VK_5 = &H35
    Public Const VK_6 = &H36
    Public Const VK_7 = &H37
    Public Const VK_8 = &H38
    Public Const VK_9 = &H39
    Public Const VK_A = &H41
    Public Const VK_B = &H42
    Public Const VK_C = &H43
    Public Const VK_D = &H44
    Public Const VK_E = &H45
    Public Const VK_F = &H46
    Public Const VK_G = &H47
    Public Const VK_H = &H48
    Public Const VK_I = &H49
    Public Const VK_J = &H4A
    Public Const VK_K = &H4B
    Public Const VK_L = &H4C
    Public Const VK_M = &H4D
    Public Const VK_N = &H4E
    Public Const VK_O = &H4F
    Public Const VK_P = &H50
    Public Const VK_Q = &H51
    Public Const VK_R = &H52
    Public Const VK_S = &H53
    Public Const VK_T = &H54
    Public Const VK_U = &H55
    Public Const VK_V = &H56
    Public Const VK_W = &H57
    Public Const VK_X = &H58
    Public Const VK_Y = &H59
    Public Const VK_Z = &H5A
    Public Const VK_STARTKEY = &H5B
    Public Const VK_CONTEXTKEY = &H5D
    Public Const VK_NUMPAD0 = &H60
    Public Const VK_NUMPAD1 = &H61
    Public Const VK_NUMPAD2 = &H62
    Public Const VK_NUMPAD3 = &H63
    Public Const VK_NUMPAD4 = &H64
    Public Const VK_NUMPAD5 = &H65
    Public Const VK_NUMPAD6 = &H66
    Public Const VK_NUMPAD7 = &H67
    Public Const VK_NUMPAD8 = &H68
    Public Const VK_NUMPAD9 = &H69
    Public Const VK_MULTIPLY = &H6A
    Public Const VK_ADD = &H6B
    Public Const VK_SEPARATOR = &H6C
    Public Const VK_SUBTRACT = &H6D
    Public Const VK_DECIMAL = &H6E
    Public Const VK_DIVIDE = &H6F
    Public Const VK_F1 = &H70
    Public Const VK_F2 = &H71
    Public Const VK_F3 = &H72
    Public Const VK_F4 = &H73
    Public Const VK_F5 = &H74
    Public Const VK_F6 = &H75
    Public Const VK_F7 = &H76
    Public Const VK_F8 = &H77
    Public Const VK_F9 = &H78
    Public Const VK_F10 = &H79
    Public Const VK_F11 = &H7A
    Public Const VK_F12 = &H7B
    Public Const VK_F13 = &H7C
    Public Const VK_F14 = &H7D
    Public Const VK_F15 = &H7E
    Public Const VK_F16 = &H7F
    Public Const VK_F17 = &H80
    Public Const VK_F18 = &H81
    Public Const VK_F19 = &H82
    Public Const VK_F20 = &H83
    Public Const VK_F21 = &H84
    Public Const VK_F22 = &H85
    Public Const VK_F23 = &H86
    Public Const VK_F24 = &H87
    Public Const VK_NUMLOCK = &H90
    Public Const VK_OEM_SCROLL = &H91
    Public Const VK_OEM_1 = &HBA
    Public Const VK_OEM_PLUS = &HBB
    Public Const VK_OEM_COMMA = &HBC
    Public Const VK_OEM_MINUS = &HBD
    Public Const VK_OEM_PERIOD = &HBE
    Public Const VK_OEM_2 = &HBF
    Public Const VK_OEM_3 = &HC0
    Public Const VK_OEM_4 = &HDB
    Public Const VK_OEM_5 = &HDC
    Public Const VK_OEM_6 = &HDD
    Public Const VK_OEM_7 = &HDE
    Public Const VK_OEM_8 = &HDF
    Public Const VK_ICO_F17 = &HE0
    Public Const VK_ICO_F18 = &HE1
    Public Const VK_OEM102 = &HE2
    Public Const VK_ICO_HELP = &HE3
    Public Const VK_ICO_00 = &HE4
    Public Const VK_ICO_CLEAR = &HE6
    Public Const VK_OEM_RESET = &HE9
    Public Const VK_OEM_JUMP = &HEA
    Public Const VK_OEM_PA1 = &HEB
    Public Const VK_OEM_PA2 = &HEC
    Public Const VK_OEM_PA3 = &HED
    Public Const VK_OEM_WSCTRL = &HEE
    Public Const VK_OEM_CUSEL = &HEF
    Public Const VK_OEM_ATTN = &HF0
    Public Const VK_OEM_FINNISH = &HF1
    Public Const VK_OEM_COPY = &HF2
    Public Const VK_OEM_AUTO = &HF3
    Public Const VK_OEM_ENLW = &HF4
    Public Const VK_OEM_BACKTAB = &HF5
    Public Const VK_ATTN = &HF6
    Public Const VK_CRSEL = &HF7
    Public Const VK_EXSEL = &HF8
    Public Const VK_EREOF = &HF9
    Public Const VK_PLAY = &HFA
    Public Const VK_ZOOM = &HFB
    Public Const VK_NONAME = &HFC
    Public Const VK_PA1 = &HFD
    Public Const VK_OEM_CLEAR = &HFE
    Public Const KEYEVENTF_EXTENDEDKEY = &H1
    Public Const KEYEVENTF_KEYUP = &H2
     
    Sub Main() 
    
        ' -- Find the window you want to send keystrokes to and give it
    focus
        keybd_event VK_NUMPAD0, 0, 0, 0               ' Press the 0 key on
    the numpad
        keybd_event VK_NUMPAD0, 0, KEYEVENTF_KEYUP, 0 ' Release the 0 key on
    the numpad
    
    End Sub
    
    -----Original Message-----
    From: dotnet-owner@xxxxxxxxxxx [mailto:dotnet-owner@xxxxxxxxxxx] On
    Behalf Of Jarrod Oakley
    Sent: Thursday, 3 March 2005 10:07 AM
    To: dotnet@xxxxxxxxxxx
    Subject: Re: [aus-dotnet] simulating a keypress
    
    Thanks Stephen, you've been most helpful,
    
    Jarrod
    
    ----- Original Message -----
    From: "Stephen" <stephen.krauklis@xxxxxxxxx>
    To: <dotnet@xxxxxxxxxxx>
    Sent: Wednesday, March 02, 2005 7:12 PM
    Subject: Re: [aus-dotnet] simulating a keypress
    
    
    > basically the code goes as follows;
    >
    > Dim lnghWnd As Long
    > Dim lngResultValue As Long
    > Dim lngResult As Long
    >
    > lnghWnd = API.FindWindowEx(0&, 0&, "bosa_sdm_Microsoft Word 11.0",
    > "Invalid Merge Field")
    >
    > If lnghWnd Then
    >
    >     ' Send these to the window
    >     lngResultValue = API.PostMessage(lnghWnd, API.WM_SYSKEYDOWN,
    > API.VK_MENU, vbNull)
    >     lngResultValue = API.PostMessage(lnghWnd, API.WM_SYSKEYDOWN,
    > Asc("R"), vbNull)
    >     lngResultValue = API.PostMessage(lnghWnd, API.WM_SYSCHAR,
    Asc("R"), 
    > vbNull)
    >
    >     lngResult = API.WaitForSingleObject(lnghWnd, API.INFINITE)
    >
    > EndIf
    > This code actaully send ALT-R,R to the Invalid Merge Feidls Window of
    > Microsoft Word 2003
    >
    > The API Class is a wrapper for the Win32api. ( I have attached it
    > also, but not all of the constants have been included i.e. Const
    > VK_NUMPAD8 = &H68
    > ).
    >
    > Just looking at the Message Logging from Spy++ the following messages
    > are send when pressing numeric keys to a Microsoft Word Document:
    >
    > Numeric Keypad '8' produces:
    >
    > <00001> 003A06B6 P WM_KEYDOWN nVirtKey:VK_NUMPAD8 cRepeat:1
    > ScanCode:48 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
    > <00002> 003A06B6 P WM_CHAR chCharCode:'8' (56) cRepeat:1 ScanCode:48
    > fExtended:0 fAltDown:0 fRepeat:0 fUp:0
    > <00003> 003A06B6 P WM_KEYUP nVirtKey:VK_NUMPAD8 cRepeat:1 ScanCode:48
    > fExtended:0 fAltDown:0 fRepeat:1 fUp:1
    >
    > Top Row Number '8' produces:
    >
    > <00004> 003A06B6 P WM_KEYDOWN nVirtKey:'8' cRepeat:1 ScanCode:09
    > fExtended:0 fAltDown:0 fRepeat:0 fUp:0
    > <00005> 003A06B6 P WM_CHAR chCharCode:'8' (56) cRepeat:1 ScanCode:09
    > fExtended:0 fAltDown:0 fRepeat:0 fUp:0
    > <00006> 003A06B6 P WM_KEYUP nVirtKey:'8' cRepeat:1 ScanCode:09
    > fExtended:0 fAltDown:0 fRepeat:1 fUp:1
    >
    > Check out the PostMessage Function because a single keypress can be
    > sent as required. Depending on the app you are trying to control the
    > values in FindWindowEx and PostMessage will be different.
    >
    > Again - check out Spy++ as it can reveal alot about what is happening
    > to each window.
    >
    > I hope this helps.
    >
    > S.
    >
    > On Wed, 2 Mar 2005 17:49:43 +1000, Jarrod Oakley <jarrod@xxxxxxxxxxxx>
    
    > wrote:
    >> Stephen,
    >>
    >> That would be great if you could give me a piece of your sample code.
    >>
    >> But surely there is an easier way of doing this??  There is a code
    for
    >> almost every key on the keyboard... there must be something to 
    >> differentiate
    >> between the normal numbers and the numpad numbers without having to
    dig 
    >> down
    >> to lower level functions like
    >>
    sendMessage!(http://msdn.microsoft.com/library/default.asp?url=/library/
    en-us/cpref/html/frlrfSystemWindowsFormsSendKeysClassTopic.asp)
    >>
    >> Jarrod
    >>
    >> ----- Original Message -----
    >> From: "Stephen" <stephen.krauklis@xxxxxxxxx>
    >> To: <dotnet@xxxxxxxxxxx>
    >> Sent: Wednesday, March 02, 2005 3:27 PM
    >> Subject: Re: [aus-dotnet] simulating a keypress
    >>
    >> > On the serious side - I had the same issue many days ago - Have you
    >> > seen Spy++? It is a tool that can record messages going to and from
    >> > Windows. I used it to determine the dialog names for Word Popups
    >> > whereby I would send the appropriate keystrokes to perform the
    >> > appropriate actions and thus dispose of the dialog (during
    automation
    >> > processes).
    >> >
    >> > I have some VB code that uses the Win32api to do it and it wouldnt
    be
    >> > much different in .NET
    >> >
    >> > Let me know of you are interested.
    >> >
    >> > S.
    >> >
    >> >
    >> > On Wed, 2 Mar 2005 16:23:29 +1100, Stephen
    <stephen.krauklis@xxxxxxxxx>
    >> > wrote:
    >> >> Sorry. On first sight, I thought the subject line referred to a
    >> >> "S't'imulating" Keypress - I wanted to know where you purchase
    these
    >> >> keyboards....
    >> >>
    >> >>
    >> >> On Wed, 2 Mar 2005 16:04:53 +1000, Jarrod Oakley
    <jarrod@xxxxxxxxxxxx>
    >> >> wrote:
    >> >> > Aaron,
    >> >> >
    >> >> > No, it doesn't seem to work, it passes key number (ie 97 for 
    >> >> > Numpad1),
    >> >> > which
    >> >> > is seen by the receiving application as 97.
    >> >> >
    >> >> > Any other ideas?
    >> >> >
    >> >> >
    >> >> > ----- Original Message -----
    >> >> > From: "Aaron Cripps" <aaron.cripps@xxxxxxxxx>
    >> >> > To: <dotnet@xxxxxxxxxxx>
    >> >> > Sent: Wednesday, March 02, 2005 1:23 PM
    >> >> > Subject: Re: [aus-dotnet] simulating a keypress
    >> >> >
    >> >> > > Jarrod,
    >> >> > >
    >> >> > > I've not used sendkeys before so this may be totally wrong but
    
    >> >> > > maybe
    >> >> > > System.Windows.Forms.Keys enumeration?
    >> >> > > Keys.Numpad1
    >> >> > > Keys.Numpad2
    >> >> > > etc.
    >> >> > >
    >> >> > > Aaron
    >> >> > >
    >> >> > > On Wed, 2 Mar 2005 14:00:52 +1000, Jarrod Oakley
    >> >> > > <jarrod@xxxxxxxxxxxx>
    >> >> > > wrote:
    >> >> > >> Cheers,
    >> >> > >>
    >> >> > >> I've just got one more problem though... Using sendKeys I
    need to
    >> >> > >> send
    >> >> > >> numbers from the numpad, not the numbers across the top of
    the
    >> >> > >> keyboard.
    >> >> > >> I've searched far and wide, but no one seems to have an
    answer.
    >> >> > >>
    >> >> > >> Any ideas?
    >> >> > >>
    >> >> > >> Jarrod
    >> >> > >>
    >> >> > >>
    >> >> > >> ----- Original Message -----
    >> >> > >> From: Roger Curtain
    >> >> > >> To: dotnet@xxxxxxxxxxx
    >> >> > >> Sent: Tuesday, March 01, 2005 1:25 PM
    >> >> > >> Subject: Re: [aus-dotnet] simulating a keypress
    >> >> > >>
    >> >> > >> Jarrod, I have used several methods in the past.
    >> >> > >>
    >> >> > >>     1. Set the focus to the application in question and use
    >> >> > >> SendKeys.
    >> >> > >>     2. If the application has a menu shortcut for the action
    use
    >> >> > >> that by
    >> >> > >> simulating a menu click.
    >> >> > >>     3. If a button on the application also issues the command
    
    >> >> > >> then
    >> >> > >> press
    >> >> > >> that using code.
    >> >> > >>
    >> >> > >> ALL THE ABOVE REQUIRE WIN32 api WORK
    >> >> > >>
    >> >> > >>
    >> >> > >>     The trouble is the keyboard is global and the keystrokes
    go 
    >> >> > >> to
    >> >> > >> the
    >> >> > >> app
    >> >> > >> which has the focus, unless you have installed a global
    keyboard
    >> >> > >> hook and
    >> >> > >> intercepted the keystrokes. Not useful in this case.
    >> >> > >>
    >> >> > >> Regards Roger
    >> >> > >>
    >> >> > >> ----- Original Message -----
    >> >> > >> From: Jarrod Oakley
    >> >> > >> To: dotnet@xxxxxxxxxxx
    >> >> > >> Sent: Tuesday, March 01, 2005 1:04 PM
    >> >> > >> Subject: [aus-dotnet] simulating a keypress
    >> >> > >>
    >> >> > >> Hi,
    >> >> > >>
    >> >> > >> I need to simulate a keypress event from a .net application
    to
    >> >> > >> another
    >> >> > >> windows based application (it happens to be a phone
    emulator). 
    >> >> > >> Is
    >> >> > >> there
    >> >> > >> a
    >> >> > >> way of directing a sendKeys.send() event to a specified
    window 
    >> >> > >> that
    >> >> > >> simulates a keypress from within that applciation?
    >> >> > >>
    >> >> > >> Thanks,
    >> >> > >>
    >> >> > >> Jarrod
    >> >> > > _________________
    >> >> > > You are a part of the Australian "dotnet" mailing list.
    >> >> > > To unsubscribe send "unsubscribe dotnet" or to re-subscribe
    send
    >> >> > > "subscribe dotnet Your Name" in the body of the email to:
    >> >> > > imailsrv@xxxxxxxxxxx
    >> >> > > List managed by: http://www.stanski.com
    >> >> > >
    >> >> >
    >> >> > _________________
    >> >> > You are a part of the Australian "dotnet" mailing list.
    >> >> > To unsubscribe send "unsubscribe dotnet" or to re-subscribe send
    >> >> > "subscribe dotnet Your Name" in the body of the email to:
    >> >> > imailsrv@xxxxxxxxxxx
    >> >> > List managed by: http://www.stanski.com
    >> >> >
    >> >>
    >> >> --
    >> >>
    >> >> Mercantile Systems
    >> >> http://www.mspl.com
    >> >>
    >> >
    >> >
    >> > --
    >> >
    >> > Mercantile Systems
    >> > http://www.mspl.com
    >> > _________________
    >> > You are a part of the Australian "dotnet" mailing list.
    >> > To unsubscribe send "unsubscribe dotnet" or to re-subscribe send
    >> > "subscribe dotnet Your Name" in the body of the email to:
    >> > imailsrv@xxxxxxxxxxx
    >> > List managed by: http://www.stanski.com
    >> >
    >>
    >> _________________
    >> You are a part of the Australian "dotnet" mailing list.
    >> To unsubscribe send "unsubscribe dotnet" or to re-subscribe send 
    >> "subscribe dotnet Your Name" in the body of the email to: 
    >> imailsrv@xxxxxxxxxxx
    >> List managed by: http://www.stanski.com
    >>
    >
    >
    > -- 
    >
    > Mercantile Systems
    > http://www.mspl.com
    > 
    
    
    _________________
    You are a part of the Australian "dotnet" mailing list.
    To unsubscribe send "unsubscribe dotnet" or to re-subscribe send
    "subscribe dotnet Your Name" in the body of the email to:
    imailsrv@xxxxxxxxxxx
    List managed by: http://www.stanski.com
    
    
    
    
    NOTICE
    The information contained in this electronic mail message is privileged and confidential, and is intended only for use of the addressee.  If you are not the intended recipient, you are hereby notified that any disclosure, reproduction, distribution or other use of this communication is strictly prohibited.  If you have received this communication in error, please notify the sender by reply transmission and delete the message without copying or disclosing it.
    
    _________________
    You are a part of the Australian "dotnet" mailing list.
    To unsubscribe send "unsubscribe dotnet" or to re-subscribe send "subscribe dotnet Your Name" in the body of the email to: imailsrv@xxxxxxxxxxx
    List managed by: http://www.stanski.com
    
    



    (Click here for more information on the aus-dotnet mailling list)