Scripting

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Michael Vogel
Addict
Addict
Posts: 2807
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Scripting

Post by Michael Vogel »

When I read about using AutoHotKey for PureBasic I just combined some procedures to create a program for doing simple scripts in text based programs.

If you want to play around, just copy all files from Script Bird into a directory, start the exe file and press Win+Shift+S to see the configuration panel.

To play a macro, just press Win+S, followed by a character and a number, for example "D1" which should bring you a DataSection text. To see more advanced features, try the scripts "P1","E3" and "I1"...

The following snippet shows the main code for sending keys...

Code: Select all

Procedure SendPrepare(*n.Pointer,VK,Char,UpDown,*Resize.Pointer=#Null)
		SendInputData(*n\Value)\Type=#INPUT_KEYBOARD
		SendInputData(*n\Value)\ki\wVk=VK
		SendInputData(*n\Value)\ki\time=0
		SendInputData(*n\Value)\ki\dwExtraInfo=0
		SendInputData(*n\Value)\ki\wScan=Char
		SendInputData(*n\Value)\ki\dwFlags=UpDown
		*n\Value+1
		If *Resize
			*Resize\Value+1
			ReDim SendInputData(*Resize\Value)
		EndIf
	EndProcedure
	Procedure SendInput(Text.s)

		#KEYEVENTF_UNICODE = 4

		Protected i.l
		Protected j.l
		Protected k.l

		Protected Char.l
		Protected NewChar.l
		Protected KeyDown.l

		k=Len(Text)

		If k
			ReDim SendInputData(k)

			While j < k
				MultiByteToWideChar_(#CP_ACP,0,@Text+i,1,@newchar,1)

				If (NewChar=Char) And KeyDown

					KeyDown=#False
					SendPrepare(@j,0,Char,#KEYEVENTF_UNICODE|#KEYEVENTF_KEYUP,@k)

				Else

					i+1
					Char=NewChar

					Select Char
					Case #TAB
						SendPrepare(@j,#VK_TAB,0,#KEYEVENTF_EXTENDEDKEY)
					Case #Space
						SendPrepare(@j,#VK_SPACE,0,#KEYEVENTF_EXTENDEDKEY)
					Case #CR
						KeyDown=OptExcelReturn
						If OptEnterFlag And j=k-1
							KeyDown=#False
						EndIf
						If KeyDown
							SendPrepare(@j,#VK_LMENU,0,#KEYEVENTF_EXTENDEDKEY,@k)
						EndIf
						SendPrepare(@j,#VK_RETURN,0,#KEYEVENTF_EXTENDEDKEY)
						If KeyDown
							SendPrepare(@j,#VK_LMENU,0,#KEYEVENTF_EXTENDEDKEY|#KEYEVENTF_KEYUP,@k)
						EndIf
					Case #ESC
						SendPrepare(@j,#VK_ESCAPE,0,#KEYEVENTF_EXTENDEDKEY)
					Case #Code_Left
						SendPrepare(@j,#VK_LEFT,0,#KEYEVENTF_EXTENDEDKEY)
					Case #Code_Right
						SendPrepare(@j,#VK_RIGHT,0,#KEYEVENTF_EXTENDEDKEY)
					Case #Code_Up
						SendPrepare(@j,#VK_UP,0,#KEYEVENTF_EXTENDEDKEY)
					Case #Code_Down
						SendPrepare(@j,#VK_DOWN,0,#KEYEVENTF_EXTENDEDKEY)
					Case #Code_Home
						SendPrepare(@j,#VK_HOME,0,#KEYEVENTF_EXTENDEDKEY)
					Case #Code_End
						SendPrepare(@j,#VK_END,0,#KEYEVENTF_EXTENDEDKEY)
					Default
						SendPrepare(@j,0,Char,#KEYEVENTF_UNICODE)
					EndSelect

					KeyDown=#True

				EndIf
			Wend

			SendInput_(k,SendInputData(0),SizeOf(INPUT))

		EndIf

	EndProcedure