Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for pmchoice.kix

(view source code of pmchoice.kix as plain text)

  1. ; PMCHOICE.KIX,  Version 2.00 for Windows NT
  2. ; Poor Man's replacement for the CHOICE command from the NT Resource Kit.
  3. ; Needs PMCHOICE.BAT to convert original parameters to Kix variables.
  4. ; Requires KiXtart 4.01 or later if a timeout is specified.
  5. ; Batch/KiXtart combination follows the original CHOICE.EXE syntax.
  6. ; Written by Rob van der Woude
  7. ; http://www.robvanderwoude.com
  8.  
  9. ; Show text if required
  10. If $Text <> ""
  11. 	$Text
  12. EndIf
  13.  
  14. ; Show prompt if required
  15. If $NoPrompt <> 1
  16. 	"? [" + $Choices + "]"
  17. EndIf
  18.  
  19. :Start
  20. If $TimeOut = 0
  21. 	; Wait infinitely for key to be pressed
  22. 	Get $Key
  23. Else
  24. 	; Requires at least KiXtart 4.01
  25. 	$KixMajorVer = SubStr( @Kix, 1, InStr( @Kix, "." ) - 1 )
  26. 	If $KixMajorVer < 4
  27. 		Cls
  28. 		? "PMChoice.kix requires KiXtart version 4.01 or later."
  29. 		? "Your KiXtart version is " + @KIX + "."
  30. 		?
  31. 		Quit 255
  32. 	EndIf
  33. 	; Check if key is pressed
  34. 	If KbHit( )
  35. 		Get $Key
  36. 	Else
  37. 		; Check if key is pressed each second during timeout interval
  38. 		$Counter = 0
  39. 		; Default in case no key will be pressed before end of timeout
  40. 		$Key     = Chr( 13 )
  41. 		Do
  42. 			Sleep 1
  43. 			$Counter = $Counter + 1
  44. 			If KbHit( )
  45. 				; Once pressed, read and then exit loop
  46. 				Get $Key
  47. 				$Counter = $TimeOut
  48. 			EndIf
  49. 		Until $Counter = $TimeOut
  50. 	EndIf
  51. EndIf
  52.  
  53. ; Pressing Esc results in return code 0
  54. If $Key = Chr( 27 )
  55. 	Exit 0
  56. EndIf
  57.  
  58. ; Pressing Enter results in default (if defined!)
  59. If $Key = Chr( 13 )
  60. 	$Key = $Default
  61. EndIf
  62.  
  63. ; The return code must be the position of the key in the choices string
  64. $Return = InStr( "$Choices", "$Key" )
  65.  
  66. ; Beep and start again if an invalid key was pressed
  67. If $Return = 0
  68. 	Beep
  69. 	GoTo Start
  70. EndIf
  71.  
  72. ; If cases sensitivity was defined, check if the cases match
  73. If $Cs = "1"
  74. 	If Asc( SubStr( "$Choices", $Return, 1 ) ) <> Asc( $Key )
  75. 		Beep
  76. 		GoTo Start
  77. 	EndIf
  78. EndIf
  79.  
  80. ; Exit with the proper return code
  81. Exit $Return
  82.  

page last modified: 2024-04-16; loaded in 0.0066 seconds