SendKeys sub

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

SendKeys sub

Post by marpon »

Hy

it is my sample for Sendkeys : I hope it can work with all international keyboards,
to test just compile with console and you have 10 secondes to open notepad or other text aplication


updated code

Code: Select all

#include once "windows.bi"
'function caps_state()as integer
'	Dim as short lret
'	lret=LoByte(GetKeyState(VK_CAPITAL))
'	if lret THEN
'		messagebox 0,"Caps state : " & lret,"info",0
'	else
'		messagebox 0,"Caps Off","info",0	
'   END IF
'	return cast(integer,lret)
'END function


sub lanceT(ccode as integer)
	dim as integer ishift, ialt , ictrl
   Dim as integer icod, kcod,lcod,hcod
	icod= VkKeyScan(ccode)
	if icod =-1 THEN
		Keybd_Event(0, ccode, KEYEVENTF_UNICODE, 0)
		''sleep 1
		Keybd_Event(0, ccode, KEYEVENTF_UNICODE or KEYEVENTF_KEYUP, 0)
		'sleep 1
		
		exit sub
   END IF
	'print str(ccode) & "  " & icod & "  " 
	lcod= LoByte( icod)
	hcod= HiByte(icod)
	kcod= MapVirtualKey(lcod, 0)
	if hcod =7  THEN
		ishift=1 : ialt =1 : ictrl=1
	elseif hcod =6  THEN
		ishift=0 : ialt =1 : ictrl=1
	elseif hcod =5  THEN
		ishift=1 : ialt =1 : ictrl=0
	elseif hcod =4  THEN
		ishift=0 : ialt =1 : ictrl=0	
	elseif hcod =3  THEN
		ishift=1 : ialt =0 : ictrl=1
	elseif hcod =2  THEN
		ishift=0 : ialt =0 : ictrl=1
	elseif hcod =1  THEN
		ishift=1 : ialt =0 : ictrl=0
	elseif hcod =0  THEN
		ishift=0 : ialt =0 : ictrl=0								 
	END IF
	if ialt>0 THEN Keybd_Event(VK_MENU, 0, 0, 0)
	'sleep 1
	if ictrl>0 THEN Keybd_Event(VK_CONTROL, 0, 0, 0)
	 'sleep 1
	if ishift>0 THEN Keybd_Event(VK_SHIFT, 0, 0, 0)
	'sleep 1

	Keybd_Event(lcod, kcod, 0, 0)
	'sleep 1
	Keybd_Event(lcod, kcod, KEYEVENTF_KEYUP, 0)
	'sleep 1

	if ishift>0 THEN Keybd_Event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0)
	'sleep 1
	if ictrl>0 THEN Keybd_Event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
	'sleep 1
	if ialt>0 THEN Keybd_Event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
	'sleep 1
	
END SUB



Sub SendKeys(ByVal sText As String)
  Dim VK As WCHAR
  Dim As Integer VK_BAK = &H8
  Dim As Integer VK_WIN = &H5B 
  Dim sChar As String
  Dim i As Integer
  Dim as short mlret
  
  mlret=LoByte(GetKeyState(VK_CAPITAL))
  if mlret= 1 THEN
	  Keybd_Event(VK_CAPITAL, 0, 0, 0)
	  Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0)
  END IF
  For i = 1 To Len(sText)
    sChar = Mid$(sText, i, 1)

    If sChar = "{" Then
      If UCase$(Mid$(sText, i + 1, 10)) = "BACKSPACE}" Then
        VK = VK_BAK
        i = i + 10
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "BS}" Then
        VK = VK_BAK
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "BKSP}" Then
        VK = VK_BAK
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "BREAK}" Then
        VK = VK_PAUSE
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 9)) = "CAPSLOCK}" Then
        VK = VK_CAPITAL
        i = i + 9
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "DELETE}" Then
        VK = VK_DELETE
        i = i + 7
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "DEL}" Then
        VK = VK_DELETE
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "DOWN}" Then
        VK = VK_DOWN
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "UP}" Then
        VK = VK_UP
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "LEFT}" Then
        VK = VK_LEFT
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "RIGHT}" Then
        VK = VK_RIGHT
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "END}" Then
        VK = VK_END
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "ENTER}" Then
        VK = VK_RETURN
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "HOME}" Then
        VK = VK_HOME
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "ESC}" Then
        VK = VK_ESCAPE
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "HELP}" Then
        VK = VK_HELP
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "INSERT}" Then
        VK = VK_INSERT
        i = i + 7
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "INS}" Then
        VK = VK_INSERT
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 8)) = "NUMLOCK}" Then
        VK = VK_NUMLOCK
        i = i + 8
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "PGUP}" Then
        VK = VK_PRIOR
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "PGDN}" Then
        VK = VK_NEXT
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 11)) = "SCROLLLOCK}" Then
        VK = VK_SCROLL
        i = i + 11
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "TAB}" Then
        VK = VK_TAB
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F1}" Then
        VK = VK_F1
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F2}" Then
        VK = VK_F2
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F3}" Then
        VK = VK_F3
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F4}" Then
        VK = VK_F4
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F5}" Then
        VK = VK_F5
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F6}" Then
        VK = VK_F6
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F7}" Then
        VK = VK_F7
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F8}" Then
        VK = VK_F8
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F9}" Then
        VK = VK_F9
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F10}" Then
        VK = VK_F10
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F11}" Then
        VK = VK_F11
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F12}" Then
        VK = VK_F12
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F13}" Then
        VK = VK_F13
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F14}" Then
        VK = VK_F14
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F15}" Then
        VK = VK_F15
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F16}" Then
        VK = VK_F16
        i = i + 4
      ' NEU! Windows-Taste
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "WIN}" Then
        VK = VK_WIN
        i = i + 4
      ' NEU! Kontextmenü
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "APPS}" Then
        VK = VK_APPS
        i = i + 5
      ' NEU! PrintScreen-Taste (DRUCK)
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "PRTSC}" Then
        VK = VK_SNAPSHOT
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "ALT_D}" Then
        Keybd_event VK_MENU, 0, 0, 0
		  'sleep 1
        i = i + 6
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "ALT_U}" Then
        Keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
		  'sleep 1
        i = i + 6
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "CTRL_D}" Then
        Keybd_event VK_CONTROL, 0, 0, 0
		  'sleep 1
        i = i + 7
        Continue For 
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "CTRL_U}" Then
        Keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
		  'sleep 1
        i = i + 7
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 8)) = "SHIFT_D}" Then
        Keybd_event VK_SHIFT, 0, 0, 0
		  'sleep 1
        i = i + 8
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 8)) = "SHIFT_U}" Then
        Keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
		  'sleep 1
        i = i + 8
        Continue For 
		ElseIf UCase$(Mid$(sText, i + 1, 2)) = "}}" Then
        VK = -1
		 lanceT( asc("}") )
        i = i + 2
        Continue For 	 
		ElseIf UCase$(Mid$(sText, i + 1, 2)) = "{}" Then
        VK = -1
		 lanceT( asc("{") )
        i = i + 2
        Continue For 		    
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "+}" Then
        VK = -1
		 lanceT( asc("+") )
        i = i + 2
        Continue For
		ElseIf UCase$(Mid$(sText, i + 1, 2)) = "%}" Then
        VK = -1
		 lanceT( asc("%") )
        i = i + 2
        Continue For
		ElseIf UCase$(Mid$(sText, i + 1, 2)) = "^}" Then
        VK = -1
		 lanceT( asc("^"))
		 lanceT( asc(" "))
        i = i + 2
        Continue For 	
		ElseIf UCase$(Mid$(sText, i + 1, 2)) = "~}" Then
        VK = -1
		 lanceT( asc("~") )
		 lanceT( asc(" "))
        i = i + 2
        Continue For 		 	  		  	
		End If
    ElseIf sChar = "+" Then  ' SHIFT
		Keybd_event VK_SHIFT, 0, 0, 0
		'sleep 1
      Keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
		'sleep 1
		Continue For 
    ElseIf sChar = "%" Then  ' ALT
		Keybd_event VK_MENU, 0, 0, 0
		'sleep 1
      Keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
		'sleep 1
		Continue For 
    ElseIf sChar = "^" Then  ' CONTROL
      Keybd_event VK_CONTROL, 0, 0, 0
		'sleep 1
      Keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
		'sleep 1
		Continue For
	 ElseIf sChar = "~" Then  ' ENTER
      Keybd_event VK_RETURN, 0, 0, 0
		'sleep 1
      Keybd_event VK_RETURN, 0, KEYEVENTF_KEYUP, 0
		'sleep 1
		Continue For
	 ElseIf sChar = "¨" Then
        VK = -1
		 lanceT( asc("¨"))
		 lanceT( asc(" "))
       Continue For 	
	 ElseIf sChar = "`" Then
        VK = -1
		 lanceT( asc("`"))
		 lanceT( asc(" "))
       Continue For
    Else
		VK = -1
		lanceT(asc(sChar) )
		
    End If

	 if vk<> -1  THEN
		Keybd_event VK, 0, 0, 0
		'sleep 1
		Keybd_event VK, 0, KEYEVENTF_KEYUP, 0
		'sleep 1
		vk= -1 
	 END IF
  Next i
  'lret=LoByte(GetKeyState(VK_CAPITAL))
  if mlret=1 THEN
	  Keybd_Event(VK_CAPITAL, 0, 0, 0)
	  Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0)
  END IF
  
  
End Sub 

'SLEEP 5000

'SendKeys"{CTRL_D}m{CTRL_U}"

' ex  SendKeys("abcEefl")


'end
sleep 10000

sendkeys "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzé'-è_çà)(=+°O&\~"
SendKeys("c'est la fin des Haricots découpés en 8/2~")
sendkeys "ABCDEFGHIJKLMNOPQRSTUVWXYZ~"
sendkeys "abcdefghijklmnopqrstuvwxyz~"
sendkeys "01234567890!@#$%^&*()_+{{}{}}][:€¤£""²,?;./§{~}n{+}{%}{^}ëêùûüâäÔ¨a±`‘´·~"

SendKeys("{ALT_D}{F4}{ALT_U}")

sleep
Last edited by marpon on Feb 15, 2013 16:40, edited 1 time in total.
FotonCat
Posts: 37
Joined: Nov 26, 2009 11:47
Location: Russia
Contact:

Re: SendKeys sub

Post by FotonCat »

Tested with notepad and russian cyrillic symbols (Codepage: Win1251). Works very nice!
PAEz
Posts: 15
Joined: Feb 04, 2013 14:21
Location: Australia

Re: SendKeys sub

Post by PAEz »

Awesome, Im going to have need of that.
Thanks for sharing.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

SendKeys sub updated

Post by marpon »

I have updated the code in first post to take care of the Capital key toggle position. CAPS LOCk or not and if the state is on , I pass to off and at the end of the sendkey , I return back to on.
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: SendKeys sub

Post by Kot »

Is there a way to send keys to a specified window instead of the one on top?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: SendKeys sub

Post by MichaelW »

Kot wrote:Is there a way to send keys to a specified window instead of the one on top?
Yes, if you first make the specified window "the one on top". This example uses SetForegroundWindow, but note in the remarks the restrictions on which processes can set the foreground window.

Code: Select all

#include "windows.bi"

dim as HWND hwndTarget
dim as string windowName = "target.txt - Notepad"

''------------------------------------------------------------------------------------
'' Try to find the target window based on the window name only. 
'' For a window that you create, and were you control the class
'' name, or a window where you can guess or otherwise predict the
'' class name, you can narrow the search by specifying the window
'' class name as well as the window name. Find Window returns a
'' window handle on success, or zero on failure.
''------------------------------------------------------------------------------------

hwndTarget = FindWindow( NULL,  strptr(windowName) )
print hwndTarget

if hwndTarget then
    print SetForegroundWindow( hwndTarget )
end if    

sleep
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: SendKeys sub

Post by Kot »

Strange, I thought it would be the number PID from Process Explorer... But I've got this number - and what next? I wrote a very, very primitive bot for a game I play, but I'd like to be able to do other things when it works. Does it have to make my game window active before it starts sending keys, no sending keys to a background application?
frognik79
Posts: 30
Joined: Sep 29, 2014 5:05

Re: SendKeys sub

Post by frognik79 »

Kot wrote:Strange, I thought it would be the number PID from Process Explorer... But I've got this number - and what next? I wrote a very, very primitive bot for a game I play, but I'd like to be able to do other things when it works. Does it have to make my game window active before it starts sending keys, no sending keys to a background application?
If you're using it for a game bot then use this:

Code: Select all

#include "windows.bi"

Sub SendKey(bVk As Byte)
  keybd_event(0, MapVirtualKey(bVk, 0), KEYEVENTF_SCANCODE, 0)
  Sleep 100
  keybd_event(0, MapVirtualKey(bVk, 0), KEYEVENTF_SCANCODE Or KEYEVENTF_KEYUP, 0)
End Sub
Most games don't process virtual keys so send them as scancodes instead.

As for sending keys to background windows I've had some success with using PostMessage, WM_KEYDOWN and WM_KEYUP.
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: SendKeys sub

Post by Kot »

Thanks, Marpon's code worked well, but movement keys didn't work (however, everything was OK with text editor as a target). With your method they work! My bot now can turn to another group of zombies and kill them all! :-D
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: SendKeys sub

Post by Kot »

I tried to recompile my bot with 1.04 compiler and I got many Invalid assignment/conversion in 'VK = VK_BAK' errors. Did I mess something up with my code accidentally or I have to change something to work with FreeBasic 1.04?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SendKeys sub

Post by fxm »

fbc 64-bit ?

Try this (which in any case is more logical by defining constants instead of local variables):

Code: Select all

#include once "windows.bi"
Const VK_BAK = &H8
Const VK_WIN = &H5B

'function caps_state()as integer
'   Dim as short lret
'   lret=LoByte(GetKeyState(VK_CAPITAL))
'   if lret THEN
'       messagebox 0,"Caps state : " & lret,"info",0
'   else
'       messagebox 0,"Caps Off","info",0   
'   END IF
'   return cast(integer,lret)
'END function


sub lanceT(ccode as integer)
    dim as integer ishift, ialt , ictrl
   Dim as integer icod, kcod,lcod,hcod
    icod= VkKeyScan(ccode)
    if icod =-1 THEN
        Keybd_Event(0, ccode, KEYEVENTF_UNICODE, 0)
        ''sleep 1
        Keybd_Event(0, ccode, KEYEVENTF_UNICODE or KEYEVENTF_KEYUP, 0)
        'sleep 1
       
        exit sub
   END IF
    'print str(ccode) & "  " & icod & "  "
    lcod= LoByte( icod)
    hcod= HiByte(icod)
    kcod= MapVirtualKey(lcod, 0)
    if hcod =7  THEN
        ishift=1 : ialt =1 : ictrl=1
    elseif hcod =6  THEN
        ishift=0 : ialt =1 : ictrl=1
    elseif hcod =5  THEN
        ishift=1 : ialt =1 : ictrl=0
    elseif hcod =4  THEN
        ishift=0 : ialt =1 : ictrl=0   
    elseif hcod =3  THEN
        ishift=1 : ialt =0 : ictrl=1
    elseif hcod =2  THEN
        ishift=0 : ialt =0 : ictrl=1
    elseif hcod =1  THEN
        ishift=1 : ialt =0 : ictrl=0
    elseif hcod =0  THEN
        ishift=0 : ialt =0 : ictrl=0                                 
    END IF
    if ialt>0 THEN Keybd_Event(VK_MENU, 0, 0, 0)
    'sleep 1
    if ictrl>0 THEN Keybd_Event(VK_CONTROL, 0, 0, 0)
     'sleep 1
    if ishift>0 THEN Keybd_Event(VK_SHIFT, 0, 0, 0)
    'sleep 1

    Keybd_Event(lcod, kcod, 0, 0)
    'sleep 1
    Keybd_Event(lcod, kcod, KEYEVENTF_KEYUP, 0)
    'sleep 1

    if ishift>0 THEN Keybd_Event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0)
    'sleep 1
    if ictrl>0 THEN Keybd_Event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
    'sleep 1
    if ialt>0 THEN Keybd_Event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
    'sleep 1
   
END SUB



Sub SendKeys(ByVal sText As String)
  Dim VK As WCHAR
'  Dim As Integer VK_BAK = &H8
'  Dim As Integer VK_WIN = &H5B
  Dim sChar As String
  Dim i As Integer
  Dim as short mlret
 
  mlret=LoByte(GetKeyState(VK_CAPITAL))
  if mlret= 1 THEN
      Keybd_Event(VK_CAPITAL, 0, 0, 0)
      Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0)
  END IF
  For i = 1 To Len(sText)
    sChar = Mid$(sText, i, 1)

    If sChar = "{" Then
      If UCase$(Mid$(sText, i + 1, 10)) = "BACKSPACE}" Then
        VK = VK_BAK
        i = i + 10
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "BS}" Then
        VK = VK_BAK
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "BKSP}" Then
        VK = VK_BAK
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "BREAK}" Then
        VK = VK_PAUSE
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 9)) = "CAPSLOCK}" Then
        VK = VK_CAPITAL
        i = i + 9
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "DELETE}" Then
        VK = VK_DELETE
        i = i + 7
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "DEL}" Then
        VK = VK_DELETE
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "DOWN}" Then
        VK = VK_DOWN
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "UP}" Then
        VK = VK_UP
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "LEFT}" Then
        VK = VK_LEFT
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "RIGHT}" Then
        VK = VK_RIGHT
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "END}" Then
        VK = VK_END
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "ENTER}" Then
        VK = VK_RETURN
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "HOME}" Then
        VK = VK_HOME
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "ESC}" Then
        VK = VK_ESCAPE
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "HELP}" Then
        VK = VK_HELP
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "INSERT}" Then
        VK = VK_INSERT
        i = i + 7
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "INS}" Then
        VK = VK_INSERT
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 8)) = "NUMLOCK}" Then
        VK = VK_NUMLOCK
        i = i + 8
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "PGUP}" Then
        VK = VK_PRIOR
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "PGDN}" Then
        VK = VK_NEXT
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 11)) = "SCROLLLOCK}" Then
        VK = VK_SCROLL
        i = i + 11
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "TAB}" Then
        VK = VK_TAB
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F1}" Then
        VK = VK_F1
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F2}" Then
        VK = VK_F2
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F3}" Then
        VK = VK_F3
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F4}" Then
        VK = VK_F4
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F5}" Then
        VK = VK_F5
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F6}" Then
        VK = VK_F6
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F7}" Then
        VK = VK_F7
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F8}" Then
        VK = VK_F8
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F9}" Then
        VK = VK_F9
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F10}" Then
        VK = VK_F10
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F11}" Then
        VK = VK_F11
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F12}" Then
        VK = VK_F12
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F13}" Then
        VK = VK_F13
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F14}" Then
        VK = VK_F14
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F15}" Then
        VK = VK_F15
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "F16}" Then
        VK = VK_F16
        i = i + 4
      ' NEU! Windows-Taste
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "WIN}" Then
        VK = VK_WIN
        i = i + 4
      ' NEU! Kontextmenü
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "APPS}" Then
        VK = VK_APPS
        i = i + 5
      ' NEU! PrintScreen-Taste (DRUCK)
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "PRTSC}" Then
        VK = VK_SNAPSHOT
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "ALT_D}" Then
        Keybd_event VK_MENU, 0, 0, 0
          'sleep 1
        i = i + 6
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "ALT_U}" Then
        Keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
          'sleep 1
        i = i + 6
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "CTRL_D}" Then
        Keybd_event VK_CONTROL, 0, 0, 0
          'sleep 1
        i = i + 7
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "CTRL_U}" Then
        Keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
          'sleep 1
        i = i + 7
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 8)) = "SHIFT_D}" Then
        Keybd_event VK_SHIFT, 0, 0, 0
          'sleep 1
        i = i + 8
        Continue For
      ElseIf UCase$(Mid$(sText, i + 1, 8)) = "SHIFT_U}" Then
        Keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
          'sleep 1
        i = i + 8
        Continue For
        ElseIf UCase$(Mid$(sText, i + 1, 2)) = "}}" Then
        VK = -1
         lanceT( asc("}") )
        i = i + 2
        Continue For    
        ElseIf UCase$(Mid$(sText, i + 1, 2)) = "{}" Then
        VK = -1
         lanceT( asc("{") )
        i = i + 2
        Continue For           
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "+}" Then
        VK = -1
         lanceT( asc("+") )
        i = i + 2
        Continue For
        ElseIf UCase$(Mid$(sText, i + 1, 2)) = "%}" Then
        VK = -1
         lanceT( asc("%") )
        i = i + 2
        Continue For
        ElseIf UCase$(Mid$(sText, i + 1, 2)) = "^}" Then
        VK = -1
         lanceT( asc("^"))
         lanceT( asc(" "))
        i = i + 2
        Continue For    
        ElseIf UCase$(Mid$(sText, i + 1, 2)) = "~}" Then
        VK = -1
         lanceT( asc("~") )
         lanceT( asc(" "))
        i = i + 2
        Continue For                       
        End If
    ElseIf sChar = "+" Then  ' SHIFT
        Keybd_event VK_SHIFT, 0, 0, 0
        'sleep 1
      Keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
        'sleep 1
        Continue For
    ElseIf sChar = "%" Then  ' ALT
        Keybd_event VK_MENU, 0, 0, 0
        'sleep 1
      Keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
        'sleep 1
        Continue For
    ElseIf sChar = "^" Then  ' CONTROL
      Keybd_event VK_CONTROL, 0, 0, 0
        'sleep 1
      Keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
        'sleep 1
        Continue For
     ElseIf sChar = "~" Then  ' ENTER
      Keybd_event VK_RETURN, 0, 0, 0
        'sleep 1
      Keybd_event VK_RETURN, 0, KEYEVENTF_KEYUP, 0
        'sleep 1
        Continue For
     ElseIf sChar = "¨" Then
        VK = -1
         lanceT( asc("¨"))
         lanceT( asc(" "))
       Continue For    
     ElseIf sChar = "`" Then
        VK = -1
         lanceT( asc("`"))
         lanceT( asc(" "))
       Continue For
    Else
        VK = -1
        lanceT(asc(sChar) )
       
    End If

     if vk<> -1  THEN
        Keybd_event VK, 0, 0, 0
        'sleep 1
        Keybd_event VK, 0, KEYEVENTF_KEYUP, 0
        'sleep 1
        vk= -1
     END IF
  Next i
  'lret=LoByte(GetKeyState(VK_CAPITAL))
  if mlret=1 THEN
      Keybd_Event(VK_CAPITAL, 0, 0, 0)
      Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0)
  END IF
 
 
End Sub

'SLEEP 5000

'SendKeys"{CTRL_D}m{CTRL_U}"

' ex  SendKeys("abcEefl")


'end
sleep 10000

sendkeys "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzé'-è_çà)(=+°O&\~"
SendKeys("c'est la fin des Haricots découpés en 8/2~")
sendkeys "ABCDEFGHIJKLMNOPQRSTUVWXYZ~"
sendkeys "abcdefghijklmnopqrstuvwxyz~"
sendkeys "01234567890!@#$%^&*()_+{{}{}}][:€¤£""²,?;./§{~}n{+}{%}{^}ëêùûüâäÔ¨a±`‘´·~"

SendKeys("{ALT_D}{F4}{ALT_U}")

sleep
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: SendKeys sub

Post by Kot »

fxm wrote:64-bit ?

Try this (which in any case is more logical by defining constants instead of local variables):

Code: Select all

<cut!>
        VK = VK_BAK

        VK = VK_BAK

        VK = VK_BAK
etc.
Yes, 64 bit. I left 3 of the lines above that cause following errors:

FbTemp.bas(94) error 180: Invalid assignment/conversion in 'VK = VK_BAK'
FbTemp.bas(97) error 180: Invalid assignment/conversion in 'VK = VK_BAK'
FbTemp.bas(100) error 180: Invalid assignment/conversion in 'VK = VK_PAUSE'
FbTemp.bas(103) error 180: Invalid assignment/conversion in 'VK = VK_CAPITAL'
FbTemp.bas(106) error 180: Invalid assignment/conversion in 'VK = VK_DELETE'

and so on. VK is WCHAR type, but I don't know what type are VK_PAUSE, VK_CAPITAL and the rest, probably Integer as VK_BAK and VK_WIN, which are both integers.
I changed everything to VK=WStr(VK_BAK)
but then the line

Keybd_event VK, 0, 0, 0

causes error 57: Type mismatch, at parameter 1 of KEYBD_EVENT() in 'Keybd_event VK, WStr(0), 0, 0'
so maybe VK just shouldn't be WCHAR type?
and then it spreads to some others like

FbTemp.bas(362) error 6: Expected '(', found '#' in 'Line Input #FileNum, TextLine'

Or maybe returning to 32 bit will solve the problem?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SendKeys sub

Post by fxm »

With fbc 32-bit (the program works):
VK_xyz is an INTEGER
WCHAR is an USHORT

Can you test it with fbc 64-bit:

Code: Select all

#include once "windows.bi"

#print typeof(VK_PAUSE)
#print typeof(WCHAR)
Perhaps:

Suppress anywhere Wstr():
VK=VK_xyz

Try to change the declaration of VK:
Dim VK As Ushort
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: SendKeys sub

Post by Kot »

fxm wrote:With fbc 32-bit (the program works):
VK_xyz is an INTEGER
WCHAR is an USHORT

Can you test it with fbc 64-bit:

Code: Select all

#include once "windows.bi"

#print typeof(VK_PAUSE)
#print typeof(WCHAR)
Output:
INTEGER
WSTRING * 1
fxm wrote: Perhaps:

Try to change the declaration of VK:
Dim VK As Ushort
Thanks, it worked, but new errors emerged:

FbTemp.bas(365) error 6: Expected '(', found '#' in 'Line Input #FileNum, MyLine'

I changed everywhere Line Input #FileNum, MyString to Input #FileNum, MyString and now it works, but I wonder how the hell the Line Input command should look like?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SendKeys sub

Post by fxm »

Kot wrote:FbTemp.bas(365) error 6: Expected '(', found '#' in 'Line Input #FileNum, MyLine'

I changed everywhere Line Input #FileNum, MyString to Input #FileNum, MyString and now it works, but I wonder how the hell the Line Input command should look like?
dkl,

Kot said he uses fbc 1.04.0 64-bit (could you confirm please?).
I thought this problem (with "windows.bi") solved in fbc 1.02.1!
http://www.freebasic.net/forum/viewtopi ... =3&t=23458
Post Reply