Batch Buttons / toggles?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
LifeRunner
Posts: 2
Joined: 22 Jul 2013 11:22

Batch Buttons / toggles?

#1 Post by LifeRunner » 22 Jul 2013 11:32

Hi! Just joined the forum and I'm on holiday + shitty internet and my bike was just stolen so I'm just sitting here coding some batch.

So, as the title suggests I want buttons / toggles in my batch code. Did some googling but I didn't find anything that helped me.
What I need buttons for is a joke torrent client me and a friend made, I added some menues and I have a "preferences" section where I just want people to have the ability to for example use the arrow keys to scroll through options and toggle them on/off.

Also to edit the contents and have that show in the code.

Like, you'd change the number of max. Connections per torrent from "200" to "500", and it would stay like that when you close the batch file.
Or for example
"Automatic Updates: [X]"
and you'd press space for example and turn that off.

As said this is just a joke, it doesn't download anything at all, everything is pre-made and programmed, so it's just for shits 'n' giggles.

But yeah, that's what I want enabled.
I'll upload the entire batch if anyone want to have a look at it :D

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Buttons / toggles?

#2 Post by aGerman » 22 Jul 2013 14:39

Batch files are running in a console window which is a text based window only. That means mouse input is disabled for batch processing and keystrokes of keys that don't send an ASCII character (like arrow keys) also cannot be handled via batch.

There might exist some 3rd party tools that work around these limitations. I remember the good old WBAT which is a 16 Bit application and doesn't run under x64 Windows systems though.

Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch Buttons / toggles?

#3 Post by penpen » 22 Jul 2013 16:40

A text menue could be created, receiving inputs from keyboard as done in the controller part of:
http://www.dostips.com/forum/viewtopic.php?f=3&t=4741&start=30

Code: Select all

@echo off
setlocal enableDelayedExpansion
set "up=w"
set "down=s"
set "toggle= "
set "quit=q"
set "key="
set "choice=1"
set "saveFile=save.bat"

set BUTTON.length=3
call :newButton BUTTON[1] "TextButton 1" "false"
call :newButton BUTTON[2] "TextButton 2" "false"
call :newButton BUTTON[3] "TextButton 3" "false"

if exist %saveFile% call %saveFile%

:loop
   cls
   for /l %%a in (1,1,%BUTTON.length%) do (
      if !choice! == %%a ( set /p "= * " < nul
      ) else ( set /p "=   " < nul
      )
      if "!BUTTON[%%a].checked!" == "true" ( set /p "=[X] " < nul
      ) else ( set /p "=[ ] " < nul
      )
      echo !BUTTON[%%a].text!
   )
   echo Press q to quit.

   for /f "delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
      if not defined key set "key=%%A"
   )
   set "key=!key:~-1!"

   if defined key (
      if "!key!" == "%up%" (
         set /A "choice-=1"
         if !choice! LSS 1 set /A "choice+=%BUTTON.length%"
      )
      if "!key!" == "%down%" (
         set /A "choice+=1"
         if !choice! GTR %BUTTON.length% set /A "choice-=%BUTTON.length%"
      )
      if "!key!" == "%toggle%" (
         if "!BUTTON[%choice%].checked!" == "true" ( set "BUTTON[%choice%].checked=false"
         ) else ( set "BUTTON[%choice%].checked=true"
         )
      )
      if "!key!" == "%quit%" goto :exit
      set "key="
   )
   goto :loop

:exit
   (
      echo set "BUTTON[1].checked=%BUTTON[1].checked%"
      echo set "BUTTON[2].checked=%BUTTON[2].checked%"
      echo set "BUTTON[3].checked=%BUTTON[3].checked%"
   ) > %saveFile%
   

   echo Good bye.
   endlocal
   goto :eof



:newButton
::   %1 Variable name to store the text toggle button
::   %2 button name
::   %3 (optional) toggle state in { true, false }
   set "%1.text=%~2"
   if "%~3" == "true" set "%1.checked=%~3"
   if "%~3" == "false" set "%1.checked=%~3"
   if not defined %1.checked set "%1.checked=false"
   goto :eof

Controls:
-up w
-down s
-toggle SPACE
-quit q

penpen

LifeRunner
Posts: 2
Joined: 22 Jul 2013 11:22

Re: Batch Buttons / toggles?

#4 Post by LifeRunner » 24 Jul 2013 07:37

penpen wrote:A text menue could be created, receiving inputs from keyboard as done in the controller part of:
http://www.dostips.com/forum/viewtopic.php?f=3&t=4741&start=30

Code: Select all

@echo off
setlocal enableDelayedExpansion
set "up=w"
set "down=s"
set "toggle= "
set "quit=q"
set "key="
set "choice=1"
set "saveFile=save.bat"

set BUTTON.length=3
call :newButton BUTTON[1] "TextButton 1" "false"
call :newButton BUTTON[2] "TextButton 2" "false"
call :newButton BUTTON[3] "TextButton 3" "false"

if exist %saveFile% call %saveFile%

:loop
   cls
   for /l %%a in (1,1,%BUTTON.length%) do (
      if !choice! == %%a ( set /p "= * " < nul
      ) else ( set /p "=   " < nul
      )
      if "!BUTTON[%%a].checked!" == "true" ( set /p "=[X] " < nul
      ) else ( set /p "=[ ] " < nul
      )
      echo !BUTTON[%%a].text!
   )
   echo Press q to quit.

   for /f "delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
      if not defined key set "key=%%A"
   )
   set "key=!key:~-1!"

   if defined key (
      if "!key!" == "%up%" (
         set /A "choice-=1"
         if !choice! LSS 1 set /A "choice+=%BUTTON.length%"
      )
      if "!key!" == "%down%" (
         set /A "choice+=1"
         if !choice! GTR %BUTTON.length% set /A "choice-=%BUTTON.length%"
      )
      if "!key!" == "%toggle%" (
         if "!BUTTON[%choice%].checked!" == "true" ( set "BUTTON[%choice%].checked=false"
         ) else ( set "BUTTON[%choice%].checked=true"
         )
      )
      if "!key!" == "%quit%" goto :exit
      set "key="
   )
   goto :loop

:exit
   (
      echo set "BUTTON[1].checked=%BUTTON[1].checked%"
      echo set "BUTTON[2].checked=%BUTTON[2].checked%"
      echo set "BUTTON[3].checked=%BUTTON[3].checked%"
   ) > %saveFile%
   

   echo Good bye.
   endlocal
   goto :eof



:newButton
::   %1 Variable name to store the text toggle button
::   %2 button name
::   %3 (optional) toggle state in { true, false }
   set "%1.text=%~2"
   if "%~3" == "true" set "%1.checked=%~3"
   if "%~3" == "false" set "%1.checked=%~3"
   if not defined %1.checked set "%1.checked=false"
   goto :eof

Controls:
-up w
-down s
-toggle SPACE
-quit q

penpen


Thanks alot mate!

Post Reply