Page 1 of 1

Keyboard Control!

Posted: Mon Jan 31, 2005 3:22 am
by Dreglor
Code updated For 5.20+

this small example code shows how to press and release the keyboard like if you were entering it your self.
everything is basicly self-explanitory or in the comments have fun :)

Code: Select all

;Keyboard Control!
;by Dreglor
;1/30/05
;notes go here for a complete chart of keycodes
;http://msdn.microsoft.com/library/Default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
RunProgram("notepad");open notepad just to test it :)
Delay(100);wait for it to load up
keybd_event_(#VK_H,0,0,0);press H
keybd_event_(#VK_H,0,#KEYEVENTF_KEYUP,0);release H
keybd_event_(#VK_E,0,0,0);press E
keybd_event_(#VK_E,0,#KEYEVENTF_KEYUP,0);release E
keybd_event_(#VK_L,0,0,0);press L
keybd_event_(#VK_L,0,#KEYEVENTF_KEYUP,0);release L
keybd_event_(#VK_L,0,0,0);press L
keybd_event_(#VK_L,0,#KEYEVENTF_KEYUP,0);release L
keybd_event_(#VK_O,0,0,0);press O
keybd_event_(#VK_O,0,#KEYEVENTF_KEYUP,0);release O
;you should see notepad popup with the text "hello" in it if not try increasing the delay

Posted: Sat Apr 16, 2005 9:23 pm
by akj
Here is an improved version:

Code: Select all

Declare SendKeys(keys$)

RunProgram("notepad") ; Open Notepad just to test it :) 
Delay(100) ; Wait for it to load up
SendKeys("HELLO")
SendKeys("GoodBye!")
; You should see notepad pop-up with text in it.  If not try increasing the delay.
End


Procedure SendKeys(keys$)
keys$+Chr(13) ; Append CRLF
For p=1 To Len(keys$)
  vkey.w=VkKeyScan_(Asc(Mid(keys$,p,1)))
  If vkey<>-1 ; If the character is defined
    state=vkey>>8 ; Shift, Ctrl and Alt states (state 6 is the same as 'Alt Gr' key)
    vkey & $FF ; Virtual key code
    If state & 1: keybd_event_(#VK_SHIFT,0,0,0): EndIf ; IF Shift
    If state & 2: keybd_event_(#VK_CONTROL,0,0,0): EndIf ; IF Ctrl
    If state & 4: keybd_event_(#VK_MENU,0,0,0): EndIf ; If Alt
    keybd_event_(vkey,0,0,0) ; Press key
    keybd_event_(vkey,0,#KEYEVENTF_KEYUP,0) ; Release key
    If state & 1: keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0): EndIf ; IF Shift
    If state & 2: keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0): EndIf ; IF Ctrl
    If state & 4: keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0): EndIf ; If Alt
  EndIf
Next p
EndProcedure

Posted: Sun Apr 17, 2005 12:06 am
by PB
And here's my SendKeys procedure from 2002:

viewtopic.php?t=3766

:)

Posted: Wed May 18, 2005 11:50 am
by omit59
Using PB:s great sendkeys procedure I'm having trouble sending "\" (chr(92)).
Seems to be sending "?" or is it just me doing something wrong???

Timo

Posted: Wed May 18, 2005 12:27 pm
by PB
> I'm having trouble sending "\" (chr(92))
> Seems to be sending "?"

Hmm, works fine for me... maybe there's a keyboard issue? I've updated
my procedure at that other link... try it again with the new code and see.

Posted: Wed May 18, 2005 3:03 pm
by omit59
Yes it might have something to do with keyboard and language (finnish).
I have tried two different XP pro computers and same result.
Any idea?

Timo

Posted: Wed May 18, 2005 3:37 pm
by PB
I'm about to go to bed but can you post the code of what you're trying to
send? Is it just something like: SendKeys(0,"Untitled - Notepad","\") ?

(Also, I wonder if I can select a Finnish layout on my English XP machine?).

Update: Yes, I can select a Finnish layout, so I'll do some tests tomorrow. :)

Posted: Thu May 19, 2005 2:04 am
by PB
Did some tests and couldn't get it working with a Finnish keyboard. :(
Maybe someone else can work out a way? I tried lots of combinations
of the LoadKeyboardLayout API and so on, but nothing seemed to work.

Posted: Thu May 19, 2005 8:48 am
by omit59
PB:
Idea was to send commands to AutoCAD LT, but it seems that it also accepts
"/" instead of "\" (in file paths). So I will try to use that and wait if anyone can
solve language problem.

Thanks anyway!