Tip: SendKeys procedure (Windows)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

This time it's very good :D

Thanks again for your precious help :wink:
ImageThe happiness is a road...
Not a destination
zikitrake
Addict
Addict
Posts: 834
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

None of the previous codes works with Spanish accentuated characters

Code: Select all

SendKeys(0,"Untitled - Notepad","AÉÍÓÚáéíóú;){ENTER}")
Now, I'm using the follow ugly code as a temporal solution:

Code: Select all

    aux.s = "El texto en cuestión en castellano/español/spanish. ¿Qué tal?"
    aux = ReplaceString(aux, "á", "´a")
    aux = ReplaceString(aux, "é", "´e")
    aux = ReplaceString(aux, "í", "´i")
    aux = ReplaceString(aux, "ó", "´o")
    aux = ReplaceString(aux, "ú", "´u")
    aux = ReplaceString(aux, "Á", "´A")
    aux = ReplaceString(aux, "É", "´E")
    aux = ReplaceString(aux, "Í", "´I")
    aux = ReplaceString(aux, "Ó", "´O")
    aux = ReplaceString(aux, "Ú", "´U")
    sendkeys(0, Tittle$, aux)
Any better/general solution for this?

Thank you and sorry for my poor english :D
MaxOhnesorg
New User
New User
Posts: 2
Joined: Mon May 06, 2013 4:18 pm

Re: Tip: SendKeys procedure (Windows)

Post by MaxOhnesorg »

I don't see anything at all
is this code outdated?
Purebasic 5.11, windows 7, german keyboard layout.

would be cool to have a sendkeys function, I have just come here from google and found that one.

yours
Max
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Tip: SendKeys procedure (Windows)

Post by Little John »

MaxOhnesorg wrote:is this code outdated?
I don't know.

However, according to my experience the AutoWin library (including its AW_SendKeys() function) works fine.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Tip: SendKeys procedure (Windows)

Post by skywalk »

The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Tip: SendKeys procedure (Windows)

Post by MachineCode »

MaxOhnesorg wrote:is this code outdated?
It looks like some characters got omitted during a forum migration or something, as there are several = and <> operators missing. Since I use this code in my own apps, I've posted the corrected code below for future readers.
MaxOhnesorg wrote:Purebasic 5.11, windows 7, german keyboard layout.
Works fine for me with PureBasic 5.11, Win 7, 64-bit. But note what PB said in his original post: it only works for English keyboards. ;)

Code: Select all

; SendKeys procedure by PB -- do whatever you want with it.  :)
; Syntax: r=SendKeys(handle,window$,keys$) ; r = 0 for failure.
; Specify either a handle or window$ title to type to, but not both!
; You cannot type curly braces { } as part of the keystrokes, sorry!

Procedure SendKeys(handle,window$,keys$)
  If window$<>"" : handle=FindWindow_(0,window$) : EndIf ; Use window$ instead of handle.
  If IsWindow_(handle)=0 ; Does the target window actually exist?
    ProcedureReturn 0 ; Nope, so report 0 for failure to type.
  Else
    ; This block gives the target window the focus before typing.
    thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0)
    thread2=GetWindowThreadProcessId_(handle,0)
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
    SetForegroundWindow_(handle) ; Target window now has the focus for typing.
    Sleep_(125) ; 1/8 second pause before typing to prevent fast CPU problems.
    ; Now the actual typing starts.
    keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT key before typing.
    keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL key before typing.
    keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT key before typing.
    keybd_event_(#VK_LWIN,0,#KEYEVENTF_KEYUP,0) ; Release WINDOWS key before typing.
    For r=1 To Len(keys$)
      vk=0 : vk$=Mid(keys$,r,1)
      If vk$="{" ; Special key found.
        s=FindString(keys$,"}",r+1)-(r+1) ; Get length of special key.
        s$=Mid(keys$,r+1,s) ; Get special key name.
        Select s$ ; Get virtual key code of special key.
          Case "ALTDOWN" : keybd_event_(#VK_MENU,0,0,0) ; Hold ALT down.
          Case "ALTUP" : keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT.
          Case "BACKSPACE" : vk=#VK_BACK
          Case "CONTROLDOWN" : keybd_event_(#VK_CONTROL,0,0,0) ; Hold CONTROL down.
          Case "CONTROLUP" : keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL.
          Case "DELAY" : vk=0 : Sleep_(1000) ; Delay typing for one second.
          Case "DELETE" : vk=#VK_DELETE
          Case "DOWN" : vk=#VK_DOWN
          Case "END" : vk=#VK_END
          Case "ENTER" : vk=#VK_RETURN
          Case "ESCAPE" : vk=#VK_ESCAPE
          Case "F1" : vk=#VK_F1
          Case "F2" : vk=#VK_F2
          Case "F3" : vk=#VK_F3
          Case "F4" : vk=#VK_F4
          Case "F5" : vk=#VK_F5
          Case "F6" : vk=#VK_F6
          Case "F7" : vk=#VK_F7
          Case "F8" : vk=#VK_F8
          Case "F9" : vk=#VK_F9
          Case "F10" : vk=#VK_F10
          Case "F11" : vk=#VK_F11
          Case "F12" : vk=#VK_F12
          Case "HOME" : vk=#VK_HOME
          Case "INSERT" : vk=#VK_INSERT
          Case "LEFT" : vk=#VK_LEFT
          Case "PAGEDOWN" : vk=#VK_NEXT
          Case "PAGEUP" : vk=#VK_PRIOR
          Case "PRINTSCREEN" : vk=#VK_SNAPSHOT
          Case "RIGHT" : vk=#VK_RIGHT
          Case "SCROLL" : vk=#VK_SCROLL
          Case "SPACE" : vk=#VK_SPACE
          Case "SHIFTDOWN" : shifted=1 : keybd_event_(#VK_SHIFT,0,0,0) ; Hold SHIFT down.
          Case "SHIFTUP" : shifted=0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT.
          Case "TAB" : vk=#VK_TAB
          Case "UP" : vk=#VK_UP
          Case "WINDOWS" : vk=#VK_LWIN
        EndSelect
        If Left(s$,3)<>"ALT" And Left(s$,7)<>"CONTROL" And Left(s$,5)<>"SHIFT"
          If vk<>0
            keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the special key.
          EndIf
        EndIf
        r+s+1 ; Continue getting the keystrokes that follow the special key.
      Else
        vk=VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found.
        If vk>303 And shifted=0 : keybd_event_(#VK_SHIFT,0,0,0) : EndIf ; Due to shifted character.
        keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the normal key.
        If vk>303 And shifted=0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) : EndIf ; Due to shifted character.
      EndIf
    Next
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf ; Finished typing to target window!
    keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT key in case user forgot.
    keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL key in case user forgot.
    keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT key in case user forgot.
    keybd_event_(#VK_LWIN,0,#KEYEVENTF_KEYUP,0) ; Release WINDOWS key in case user forgot.
    ProcedureReturn 1 ; Report successful typing!  :)
  EndIf
EndProcedure

RunProgram("notepad.exe") ; Start Notepad...
Repeat : Delay(1) : Until FindWindow_(0,"Untitled - Notepad")<>0 ; ...and wait for it to open.
SendKeys(0,"Untitled - Notepad","Doesn't PureBasic kick serious butt! ;){ENTER}")
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Tip: SendKeys procedure (Windows)

Post by yrreti »

Thanks MachineCode for posting this.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Tip: SendKeys procedure (Windows)

Post by Dude »

Hi everyone.

When I try to type "a@b.com" using the SendKeys procedure, it doesn't work with a German keyboard. Specifically, instead of typing the @ sign in the email address, it types an upper-case Q. See working snippet below.

I know the SendKeys procedure is for English keyboards only, but surely there's a way to tweak it to work with all keyboards, to send the correct keys? I've tried replacing GetKeyboardLayout_(0) with LoadKeyboardLayout() in various ways, but nothing is working.

Can anyone please help? Now that PureBasic is 100% Unicode, let's get this code international! :lol:

Code: Select all

Procedure Type(text$)

  For p=1 To Len(text$)
    vk$=Mid(text$,p,1)
    If vk$="@" : shift=0 : Else : shift=1 : EndIf
    vk=VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0))
    If vk>303 And shift=0 : keybd_event_(#VK_SHIFT,0,0,0) : EndIf
    keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0)
    If vk>303 And shift=0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) : EndIf
  Next

EndProcedure

Delay(2000)
Type("a@b.com") ; Correct on English keyboard, but types aQb.com on German keyboard.
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Tip: SendKeys procedure (Windows)

Post by DontTalkToMe »

@Dude

Initially I thought it was not possible with keybd_event() because of the shift, ctrl, alt required beyond the normal char keypress and depending on the layout but it seems I was wrong, you just have to not hardcode the key modifiers and use what VkKeyScanEx() is returning to you instead.

https://msdn.microsoft.com/en-us/librar ... 85%29.aspx

Code: Select all

Procedure Type(text$)
  Protected low, high, p, vk, vk$
  Protected l = Len(text$)
  Protected tid = 0  
  
  ; tid = GetWindowThreadProcessId_(hwin, 0) ; hwin = handle of the target window
    
  For p= 1 To l
    vk$=Mid(text$,p,1)   
    vk=VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(tid))
   
    low = vk & $ff
    high = vk >> 8
    
    If high & 1   
        keybd_event_(#VK_SHIFT,0,0,0)
    EndIf
   
    If high & 2
        keybd_event_(#VK_CONTROL,0,0,0)
    EndIf
   
    If high & 4
        keybd_event_(#VK_MENU,0,0,0)
    EndIf

    keybd_event_(low,0,0,0) : keybd_event_(low,0,#KEYEVENTF_KEYUP,0)

    If high & 1   
        keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0)
    EndIf
   
    If high & 2
        keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0)
    EndIf
   
    If high & 4
        keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0)
    EndIf
   
  Next

EndProcedure

Delay(3000) ; quick, switch to notepad

Type("a@b.com") 
Does this work ? At least for ascii chars and layouts different from USA. I tried with a French layout and it worked.


EDIT: keep in mind this do not work for Unicode characters (see SendInput() and #KEYEVENTF_UNICODE to support them) and also different threads can use different keyboard layouts (even if it's pretty rare in practice).

So GetKeyboardLayout_(0)) in reality should be GetKeyboardLayout_(tid)), where tid is the thread id of the target window.

Else if the main keyboard is USA but a particular program is using a french keyboard, GetKeyboardLayout_(0) will result in synthesizing key-presses following the USA layout in a window using a French layout.

See for example the GetWindowThreadProcessId_() added to the code.
Last edited by DontTalkToMe on Wed Jun 22, 2016 6:05 pm, edited 2 times in total.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Tip: SendKeys procedure (Windows)

Post by Dude »

DontTalkToMe wrote:Does this work ?
YES! :D I will modify it to work with the SendKeys procedure here. Thank you so much!
Toraih
New User
New User
Posts: 1
Joined: Mon Sep 15, 2003 1:39 pm
Location: Basel, Switzerland

Re: Tip: SendKeys procedure (Windows)

Post by Toraih »

This is a working extended SendKeys() with international keyboard layout support.
I've tested it sucessfully with swiss german and french layouts

I also added some additional cases for special key e.g. {-L-SHIFTDOWN} {-NUMPADENTER}

I extended the special keys part, using now {-SPECIALKEY} instead of classic {SPECIALKEY}. This allows to use the "{" key in text.
And to output the text "{-SPECIALKEY}" you can write "{{-VOID}-SPECIALKEY}".
Adjust the key-chars to your liking or to use the classic way, remove all vk2$-parts and change all +2 to +1.

Code: Select all

; Syntax: r=SendKeys(handle,window$,keys$) ; r = 0 for failure.
; Specify either a handle or window$ title to type to, but not both!
; Use {-SPECIALKEY} for pressing special key e.g. {-TAB} {-ENTER} 
Procedure SendKeys(handle,window$,keys$)
  If window$<>"" : handle=FindWindow_(0,window$) : EndIf ; Use window$ instead of handle.
  If IsWindow_(handle)=0 ; Does the target window actually exist?
    ProcedureReturn 0 ; Nope, so report 0 for failure to type.
  Else
    ; This block gives the target window the focus before typing.
    thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0)
    thread2=GetWindowThreadProcessId_(handle,0)
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
    SetForegroundWindow_(handle) ; Target window now has the focus for typing.
    Sleep_(125) ; 1/8 second pause before typing to prevent fast CPU problems.
    ; Now the actual typing starts.
    keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT key before typing.
    keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL key before typing.
    keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT key before typing.
    keybd_event_(#VK_LWIN,0,#KEYEVENTF_KEYUP,0) ; Release WINDOWS key before typing.
    For r=1 To Len(keys$)
      vk=0 : vk$=Mid(keys$,r,1)
      vk2$=Mid(keys$,r+1,1)
      If vk$="{" And vk2$="-" ; Special key found: {-
        s=FindString(keys$,"}",r+1)-(r+2) ; Get length of special key.
        s$=Mid(keys$,r+2,s)               ; Get special key name.
        ;Debug SpecialKey: "+s$
        Select s$ ; Get virtual key code of special key.
          Case "ALTDOWN" : keybd_event_(#VK_MENU,0,0,0) ; Hold ALT down.
          Case "L-ALTDOWN" : keybd_event_(#VK_LMENU,0,0,0) ; Hold ALT down.
          Case "R-ALTDOWN" : keybd_event_(#VK_RMENU,0,0,0) ; Hold ALT down. (AltGr-Key)
          Case "ALTUP" : keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT.
          Case "L-ALTUP" : keybd_event_(#VK_LMENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT.
          Case "R-ALTUP" : keybd_event_(#VK_RMENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT. (AltGr-Key)
          Case "BACKSPACE" : vk=#VK_BACK
          Case "CONTEXT" : vk=#VK_RWIN ; Open context-menu (right WIN-Key)
          Case "CONTROLDOWN" : keybd_event_(#VK_CONTROL,0,0,0) ; Hold CONTROL down.
          Case "L-CONTROLDOWN" : keybd_event_(#VK_LCONTROL,0,0,0) ; Hold left CONTROL down.
          Case "R-CONTROLDOWN" : keybd_event_(#VK_RCONTROL,0,0,0) ; Hold right CONTROL down.
          Case "CONTROLUP" : keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL.
          Case "L-CONTROLUP" : keybd_event_(#VK_LCONTROL,0,#KEYEVENTF_KEYUP,0) ; Release left CONTROL.
          Case "R-CONTROLUP" : keybd_event_(#VK_RCONTROL,0,#KEYEVENTF_KEYUP,0) ; Release right CONTROL.
          Case "DELAY" : vk=0 : Sleep_(1000) ; Delay typing for one second.
          Case "DELAY1" : vk=0 : Sleep_(1000) ; Delay typing for one second.
          Case "DELAY2" : vk=0 : Sleep_(2000) ; Delay typing for two seconds.
          Case "DELAY3" : vk=0 : Sleep_(3000) ; Delay typing for three seconds.
          Case "DELAY4" : vk=0 : Sleep_(4000) ; Delay typing for four seconds.
          Case "DELAY5" : vk=0 : Sleep_(5000) ; Delay typing for five seconds.
          Case "DELAY6" : vk=0 : Sleep_(6000) ; Delay typing for six seconds.
          Case "DELAY7" : vk=0 : Sleep_(7000) ; Delay typing for seven seconds.
          Case "DELAY8" : vk=0 : Sleep_(8000) ; Delay typing for eight seconds.
          Case "DELAY9" : vk=0 : Sleep_(9000) ; Delay typing for nine seconds.
          Case "DELAY10" : vk=0 : Sleep_(10000) ; Delay typing for ten seconds.
          Case "DELETE" : vk=#VK_DELETE
          Case "DOWN" : vk=#VK_DOWN
          Case "END" : vk=#VK_END
          Case "ENTER" : vk=#VK_RETURN
          Case "ESCAPE" : vk=#VK_ESCAPE
          Case "F1" : vk=#VK_F1
          Case "F2" : vk=#VK_F2
          Case "F3" : vk=#VK_F3
          Case "F4" : vk=#VK_F4
          Case "F5" : vk=#VK_F5
          Case "F6" : vk=#VK_F6
          Case "F7" : vk=#VK_F7
          Case "F8" : vk=#VK_F8
          Case "F9" : vk=#VK_F9
          Case "F10" : vk=#VK_F10
          Case "F11" : vk=#VK_F11
          Case "F12" : vk=#VK_F12
          Case "HOME" : vk=#VK_HOME
          Case "INSERT" : vk=#VK_INSERT
          Case "NUMPAD0" : vk=#VK_NUMPAD0
          Case "NUMPAD1" : vk=#VK_NUMPAD1
          Case "NUMPAD2" : vk=#VK_NUMPAD2
          Case "NUMPAD3" : vk=#VK_NUMPAD3
          Case "NUMPAD4" : vk=#VK_NUMPAD4
          Case "NUMPAD5" : vk=#VK_NUMPAD5
          Case "NUMPAD6" : vk=#VK_NUMPAD6
          Case "NUMPAD7" : vk=#VK_NUMPAD7
          Case "NUMPAD8" : vk=#VK_NUMPAD8
          Case "NUMPAD9" : vk=#VK_NUMPAD9
          Case "NUMPAD*" : vk=#VK_MULTIPLY
          Case "NUMPAD+" : vk=#VK_ADD
          Case "NUMPADENTER" : vk=#VK_SEPARATOR ; tbt
          Case "NUMPAD-" : vk=#VK_SUBTRACT
          Case "NUMPAD." : vk=#VK_DECIMAL
          Case "NUMPAD/" : vk=#VK_DIVIDE
          Case "LEFT" : vk=#VK_LEFT
          Case "PAGEDOWN" : vk=#VK_NEXT
          Case "PAGEUP" : vk=#VK_PRIOR
          Case "PASTE" : SendKeys(handle,"",GetClipboardText()) ; sometimes Ctrl+V isn't working, use this a workaround
          Case "PRINTSCREEN" : vk=#VK_SNAPSHOT
          Case "RIGHT" : vk=#VK_RIGHT
          Case "SCROLL" : vk=#VK_SCROLL
          Case "SPACE" : vk=#VK_SPACE
          Case "SHIFTDOWN" : shifted=1 : keybd_event_(#VK_SHIFT,0,0,0) ; Hold SHIFT down.
          Case "L-SHIFTDOWN" : shifted=1 : keybd_event_(#VK_LSHIFT,0,0,0) ; Hold left SHIFT down.
          Case "R-SHIFTDOWN" : shifted=1 : keybd_event_(#VK_RSHIFT,0,0,0) ; Hold right SHIFT down.
          Case "SHIFTUP" : shifted=0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT.
          Case "L-SHIFTUP" : shifted=0 : keybd_event_(#VK_RSHIFT,0,#KEYEVENTF_KEYUP,0) ; Release left SHIFT.
          Case "R-SHIFTUP" : shifted=0 : keybd_event_(#VK_LSHIFT,0,#KEYEVENTF_KEYUP,0) ; Release right SHIFT.
          Case "TAB" : vk=#VK_TAB
          Case "TICK" : vk=0 : Sleep_(100) ; Delay typing for 0.1 second.
          Case "TACK" : vk=0 : Sleep_(300) ; Delay typing for 0.3 second.
          Case "UP" : vk=#VK_UP
          Case "VOID" : vk=0 ; do nothing
          Case "WINDOWS" : vk=#VK_LWIN
          Default 
            ;Debug "Unkown special-key: " + s$ ; send unkown to debug
            ;SendKeys(handle,"","{-"++"}") ; or type it out
        EndSelect
        If Left(s$,3)<>"ALT" And Left(s$,7)<>"CONTROL" And Left(s$,5)<>"SHIFT"
          If vk<>0
            keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the special key.
          EndIf
        EndIf
        r+s+2 ; Continue getting the keystrokes that follow the special key.
      Else
        ;Debug "Key: "+vk$
        vk=VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(thread2)) ; Normal key found.
        low = vk & $ff
        high = vk >> 8
        If high & 1   
            keybd_event_(#VK_SHIFT,0,0,0)
        EndIf
        If high & 2
            keybd_event_(#VK_CONTROL,0,0,0)
        EndIf
        If high & 4
            keybd_event_(#VK_MENU,0,0,0)
        EndIf
        keybd_event_(low,0,0,0) : keybd_event_(low,0,#KEYEVENTF_KEYUP,0)
        If high & 1   
            keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0)
        EndIf
        If high & 2
            keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0)
        EndIf
        If high & 4
            keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0)
        EndIf
      EndIf
    Next
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf ; Finished typing to target window!
    keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT key in case user forgot.
    keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL key in case user forgot.
    keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT key in case user forgot.
    keybd_event_(#VK_LWIN,0,#KEYEVENTF_KEYUP,0) ; Release WINDOWS key in case user forgot.
    ; is it necessary to release left and right key-variants too or is the generic variant above enough? - tbt
    ProcedureReturn 1 ; Report successful typing!  :)
  EndIf
EndProcedure
To extend special key yourself, here is a nice overview of key-codes:
https://docs.microsoft.com/en-us/window ... -key-codes

have fun
-toraih
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Re: Tip: SendKeys procedure (Windows)

Post by Nico »

Use Keybd_event is not a goog idea, SendInput is more powerful, work with any keyboard (if use unicode version API) and also more simple.

Microsoft said:
The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event, mouse_event, or other calls to SendInput.
rotacak
User
User
Posts: 77
Joined: Tue Feb 14, 2006 2:00 pm

Re: Tip: SendKeys procedure (Windows)

Post by rotacak »

Anyone have any idea, why this does not work with directx/opengl games?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Tip: SendKeys procedure (Windows)

Post by Mijikai »

rotacak wrote:Anyone have any idea, why this does not work with directx/opengl games?
They most likely use other methods to handle input (ex. Direct Input or RAW Inuput).
Post Reply