New to DOS. But I have a project.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
echonight92
Posts: 5
Joined: 21 Nov 2016 17:31

New to DOS. But I have a project.

#1 Post by echonight92 » 21 Nov 2016 17:38

New member and I dont wanna take up your guys time with noob questions from noob kids but I'm trying to make a program that presses a keys at a random time. What would be the write up for this? Ive been doing research all day but for some reason I cant find a legit way to make a truly random key press script that will run on repeat. Any help is awesome.

ShadowThief
Expert
Posts: 1162
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: New to DOS. But I have a project.

#2 Post by ShadowThief » 22 Nov 2016 12:32

Depends on your ultimate goal. If you're trying to pick a random letter, that's pretty straightforward. If you're trying to keep the screensaver from kicking in/computer from locking, that's a very different approach. If you're messing with your buddies, that requires some additional code so that it isn't super obvious that a script is running.

echonight92
Posts: 5
Joined: 21 Nov 2016 17:31

Re: New to DOS. But I have a project.

#3 Post by echonight92 » 22 Nov 2016 14:29

.
Last edited by echonight92 on 23 Nov 2016 17:15, edited 2 times in total.

echonight92
Posts: 5
Joined: 21 Nov 2016 17:31

Re: New to DOS. But I have a project.

#4 Post by echonight92 » 22 Nov 2016 15:29

I was very vague. Also I did read the posting rules/ suggestions and I will add more detail. So I have a game that requires a key input every 5 seconds or so. So I just need a script that will press lets say the key J at a random time, and then presses enter. Does this help?

EVILGHO5T1
Posts: 1
Joined: 23 Nov 2016 22:23

Re: New to DOS. But I have a project.

#5 Post by EVILGHO5T1 » 23 Nov 2016 22:29

A Batch game or an "Actual" game?

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: New to DOS. But I have a project.

#6 Post by elzooilogico » 24 Nov 2016 06:47

You didn't tell if you plan to code a batch game, or want to send keys to a game you play.

This script involves spawning paralell procesess, input output redirection and calling another scripting language.

Depending what you really want there are many things to change, remove, etc. But gives you a starting point on how to achieve such a task.

Code: Select all

@echo off

:: set the file for data exchange
set "pipefile=pipefile.txt"

:: jump to subroutine
if "%~1" neq "" goto %1

:: create an empty file
del /F /Q "%pipefile%" 2>nul & copy nul "%pipefile%" >nul

:: spawn two processes
:: first one reads the data from the pipefile and processes data readed
:: second one processes user input and write it to the pipefile
start /MIN "writer" cmd /c ^""%~f0" readInput ^<"%pipefile%" ^"
start "reader" cmd /c ^""%~f0" getInput ^"

:: now this batch ends
echo(
Echo main batch end...
ping localhost -n 1 >nul
goto :EOF


:: get user input
:getInput
setlocal enableDelayedExpansion
for /l %%. in () do (
  set "input="
  set/P "input=[type X to exit] "
  echo(!input!>>"%pipefile%"
  if /I "!input!" equ "X" exit
)

:: read data from pipefile
:: if there is not data to process, performs a call to sendKeys method when interval elapses
:readInput
setlocal enableDelayedExpansion
set/a "init=0, now=0" & rem timing

for /F "tokens=2 delims=," %%i in ('tasklist /NH /FO csv /FI "windowtitle eq reader"') do set/A pid=%%i & rem get this window process id (or whatever you want to)

call :timeGet init & rem get current time

for /l %%. in () do (
  set "input="
  set /p "input="
  if defined input (
    if /I "!input!" EQU "X" exit
  ) else (
    call :timeGet now
    call :timeDiff now, init, now & rem compute time elapsed
    if !now! GTR 500 ( rem five seconds
      call :timeGet init & rem restart interval
      call :sendKeys !pid!, J
    )
  )
  ping 1.1.1.1 -w 10 -n 1 >nul 2>nul & rem avoid processor load (may be removed)
)

:: get current time in centiseconds
:timeGet centiseconds
SetLocal EnableDelayedExpansion
  for /F "tokens=1-4 delims=:.," %%A in ("!time: =0!") do set/a "cs=(((1%%A*60)+1%%B)*60+1%%C)*100+1%%D-36610100"
EndLocal & set/a "%1=%cs%"
exit/B

:: get time diff in centiseconds
:timeDiff current, previous, diff
SetLocal EnableDelayedExpansion
  set/a "diff=!%~1!-!%~2!" & if !diff! LSS 0 set/a "diff+=24*60*60*100"
EndLocal & set/a "%3=%diff%"
exit/B

:: spawn a cscript to echo desired text to the active window
:sendKeys
SetLocal
set "_vbs_file_=%TEMP%\keys.vbs"
(
  echo(set oSH ^= CreateObject("wScript.Shell"^)
rem  echo(oSH.AppActivate %1
rem  echo(wScript.Sleep 100
  echo(oSH.SendKeys "%~2"
  echo(wScript.Sleep 50
  echo(oSH.SendKeys "{ENTER}"
)>"%_vbs_file_%"
if exist "%TEMP%\keys.vbs" (set "_spawn_=%TEMP%\keys.vbs") else (set "_spawn_=keys.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
exit/B 0

echonight92
Posts: 5
Joined: 21 Nov 2016 17:31

Re: New to DOS. But I have a project.

#7 Post by echonight92 » 25 Nov 2016 14:22

Oh this is for an actual game. The batch is only to simulate someone pressing a keyboard button which is a DEV shortcut to someone actually using a card machine.

You guys are incredibly knowledgeable when it comes to this I see. This is what I have been using for another game to simulate someone touching a touch screen monitor. Its very very very simple. Could it be like it but random and not clicking but a keypress ?

Code: Select all

@echo off

rem ******************************************************************************************** rem

rem nircmd.exe sendmouse left down
rem nircmd.exe sendmouse left up

rem Waits 1 second
rem nircmd.exe wait 1000

rem moves mouse relatively to current position (x, y)
rem nirmcd.exe sendmouse move 100 200

rem Set the position of mouse cursor.
rem nircmd.exe setcursor 100 50

rem x   y
rem 420 400
rem 1220 660
rem 1220 660
rem 1220 660
rem 1180 720
rem 1180 720
rem 1180 720
rem 1180 720
rem 30 640

rem ******************************************************************************************** rem

:Start


nircmd.exe setcursor 983 435
nircmd wait 1051

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 780 420
nircmd wait 1052

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 690 540
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1700 400
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 690 400
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1500 540
nircmd wait 1051

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1000 600
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1050 540
nircmd wait 1052

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 730 300
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 710 410
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 520 600
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1600 220
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1520 330
nircmd wait 1051

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 810 550
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 250 550
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 260 530
nircmd wait 1052

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 400 620
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1420 330
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 10

nircmd.exe setcursor 1222 444
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 934 444
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 777 343
nircmd wait 1051

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 1034 662
nircmd wait 1050

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 233 444
nircmd wait 1052

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 222 1510
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 342 1523
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 983 435
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 1000 600
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 700 600
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 555 600
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 590 620
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 410 933
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100

nircmd.exe setcursor 1020 756
nircmd wait 1053

nircmd.exe sendmouse left down
nircmd wait 100
GOTO Start

echonight92
Posts: 5
Joined: 21 Nov 2016 17:31

Re: New to DOS. But I have a project.

#8 Post by echonight92 » 25 Nov 2016 14:35

Actually this is a better example because it is what I am currently using now for the game. But it is with AHK and it develops a pattern after a while, which I need it to be totally random.

thanks guys, you are the best

Code: Select all

#Persistent

counter = 0

Loop {
   Sleep, 30000
   
   Random, winner, 0, 180
   Random, pairs, 0, 100
   
   if (counter = 100) {
      counter = 1
      Send, {a}
      Send, {d}
      Send, {k}
      Send, {f}
   }
   else {
      counter += 1
   }
   
   if (pairs <= 100 and pairs > 80) {
      Send, {j}
   }
   if (pairs <= 80 and pairs > 60) {
      Send, {h}
   }
   if (pairs < 60 and pairs >= 55) {
      Send, {h}
      Send, {j}
   }
   if (winner < 55){
      Send, {z}
      Send, {k}
   }
   if (winner >= 125){
      Send, {c}
      Send, {k}
   }
   if (winner < 70 and winner >= 55){
      Send, {t}
      Send, {k}
   }
   if (winner < 125 and winner >= 110){
      Send, {u}
      Send, {k}
   }
   if (winner < 110 and winner >=100){
      Send, {g}
      Send, {k}
   }
   if (winner < 100 and winner >=90){
      Send, {y}
      Send, {k}
   }
   if (winner < 90 and winner >=70){
      Send, {x}
      Send, {k}
   }
}

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: New to DOS. But I have a project.

#9 Post by elzooilogico » 26 Nov 2016 08:02

This may help. Save as bat file, open notepad, then run the script and select the notepad window; then see what happen.

Code: Select all

@echo off
SetLocal EnableDelayedExpansion

:: get startup folder
set "root=%~dp0"

:: build a vb net exe to get the active window process ID
if not exist "%root%getWinId.exe" call :build_getWinId && del /F /Q '.exe 2>NUL

:: set the file for data exchange
set "pipefile=pipefile.txt"

if "%~1" neq "" goto %1

:: create an empty file
del /F /Q "%pipefile%" >nul 2>nul & copy nul "%pipefile%" >nul

:: spawn a process to send desired keypresses
start /B "play" cmd /c ^""%~f0" play ^<"%pipefile%" ^>nul ^"

::wait for a key press to exit
echo(
echo(Select any window to send key presses
echo(
echo(Press a key to exit this batch
echo(
timeout /T -1 >nul
echo(X>"%pipefile%"
ping localhost -n 2 -w 150>nul
echo(&echo(main batch ends...
del /F /Q "%pipefile%" >nul 2>nul
EndLocal
goto :EOF

:: the player
:play
for /f  %%A in ('%root%getWinId') do set "myID=%%~A" & rem get process id of this cmd window
set/a "counter=0, interval=300" & rem interval in centiseconds (this is 3 seconds)
rem set/a "counter=0, interval=3000" & rem interval in centiseconds (this is 30 seconds)

call :timeGet init & rem get current time

for /l %%. in () do (
  call :timeGet now & rem get current time
  call :timeDiff now, init, now

  set "input="
  set /p"input="
  if defined input if /I "!input!" EQU "X" echo(&echo(player ends...>con& exit

  for /f %%A in ('%root%getWinId') do set "pid=%%~A" & rem get process id of active window
  if !pid! NEQ !myID! ( rem only if this window is not active
    if !now! GTR !interval! (
      call :timeGet init & rem get current time
      set/a "winner=!random! * 180 / 32768 + 1, pairs=!random! * 100 / 32768 + 1"

      if !counter! EQU 100 (
        set/a counter=1
        call :sendKeys a d k f {ENTER} & rem {ENTER} sent to write newline. remove it if not nedded.
      ) else (
        set/a counter+=1
      )

      if !pairs! leq 100 if !pairs! gtr 80 (
        call :sendKeys j {ENTER}
      ) else if !pairs! leq 80 if !pairs! gtr 60 (
        call :sendKeys h {ENTER}
      ) else if !pairs! lss 60 if !pairs! geq 55 (
        call :sendKeys h j {ENTER}
      )

      if !winner! lss 55 (
        call :sendKeys z k {ENTER}
      ) else if !winner! geq 55 if !winner! lss 70 (
        call :sendKeys t k {ENTER}
      ) else if !winner! geq 70 if !winner! lss 90 (
        call :sendKeys x k {ENTER}
      ) else if !winner! geq 90 if !winner! lss 100 (
        call :sendKeys y k {ENTER}
      ) else if !winner! geq 100 if !winner! lss 110 (
        call :sendKeys g k {ENTER}
      ) else if !winner! geq 110 if !winner! lss 125 (
        call :sendKeys u k {ENTER}
      ) else ( rem if !winner! geq 125
        call :sendKeys c k {ENTER}
      )
    )
  )
)
:never reach here
goto :EOF


:: get current time in centiseconds
:timeGet centiseconds
SetLocal EnableDelayedExpansion
  for /F "tokens=1-4 delims=:.," %%A in ("!time: =0!") do set/a "cs=(((1%%A*60)+1%%B)*60+1%%C)*100+1%%D-36610100"
EndLocal & set/a "%1=%cs%"
exit/B

:: get time diff in centiseconds
:timeDiff current, previous, diff
SetLocal EnableDelayedExpansion
 set/a "diff=!%~1!-!%~2!" & if !diff! LSS 0 set/a "diff+=24*60*60*100"
EndLocal & set/a "%3=%diff%"
exit/B

:: spawn a cscript to echo desired text to the active window
:: you can send special codes as "%% (n)" ' minimize (Alt+SpaceBar,n^)
:: for more information see
:: http://ss64.com/vb/sendkeys.html
:: https://msdn.microsoft.com/es-es/library/system.windows.forms.sendkeys.send(v=vs.110).aspx
:sendKeys
SetLocal
set "_vbs_file_=%TEMP%\keys.vbs"
(
  echo(set oWS ^= CreateObject("wScript.Shell"^)
  for %%u in (%*) do (
    echo(oWS.SendKeys "%%u"
    echo(wScript.Sleep 50   ' sleep time may need to be adjusted for accuracy
  )
)>"%_vbs_file_%"
if exist "%TEMP%\keys.vbs" (set "_spawn_=%TEMP%\keys.vbs") else (set "_spawn_=keys.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
exit/B 0

:: build a vb net exe to get the active window process ID
:: see http://www.dostips.com/forum/viewtopic.php?f=3&t=5260
:build_getWinId
'>nul 2>&1||@copy /Y %windir%\System32\doskey.exe '.exe >nul
set "_vb_file_=%TEMP%\getWinId.bat"
(
  echo('^& @echo off
  echo('^& setlocal
  echo('^& set "frm=%%SystemRoot%%\Microsoft.NET\Framework\"
  echo('^& for /f "tokens=* delims=" %%%%v in ('dir /b /a:d  /o:n "%%SystemRoot%%\Microsoft.NET\Framework\v*"'^) do set netver=%%%%v
  echo('^& set vbc=%%frm%%%%netver%%\vbc.exe
  echo('^& call %%vbc%% /nologo /out:"%root%getWinId.exe" "%_vb_file_%"
  echo('^& endlocal
  echo('^& exit /b 0
  echo(
  echo(Imports System
  echo(Public Module modmain
  echo(  Public Declare Function GetForegroundWindow Lib "user32" (^) As System.IntPtr
  echo(  Public Declare Auto Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As System.IntPtr, ByVal ProcessId As UInteger^) As UInteger
  echo(  Sub Main(^)
  echo(    Dim processID As UInteger
  echo(    Dim hWnd As IntPtr = GetForegroundWindow(^)
  echo(    processID = GetWindowThreadProcessId(hWnd, processID^)
  echo(    Console.WriteLine(processID^)
  echo(   End Sub
  echo(End Module
)>"%_vb_file_%"
if exist "%TEMP%\getWinId.bat" (set "_spawn_=%TEMP%\getWinId.bat") else (set "_spawn_=getWinId.bat")
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
exit/B 0

Post Reply