Keyboard Control!

Share your advanced PureBasic knowledge/code with the community.
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Keyboard Control!

Post 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
~Dreglor
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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
Anthony Jordan
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

And here's my SendKeys procedure from 2002:

viewtopic.php?t=3766

:)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
omit59
User
User
Posts: 63
Joined: Fri Mar 11, 2005 8:41 am
Location: Finland

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
omit59
User
User
Posts: 63
Joined: Fri Mar 11, 2005 8:41 am
Location: Finland

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
omit59
User
User
Posts: 63
Joined: Fri Mar 11, 2005 8:41 am
Location: Finland

Post 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!
Post Reply