console program halt (or is there a one key input function?)

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

console program halt (or is there a one key input function?)

Post by blueznl »

can you 'halt' a console program in such a way, that it waits for a keypress but releases all available time to other programs on the machine?

the loop:

repeat
k.s=inkey()
until k>""

... does cost computing power, and waitwindowevent doesn't work because there's no window :-)... the only thing i could come up with is:

repeat
k.s=inkey()
if k=""
delay(300)
endif
until k>""

any smart alternatives? or is inkey handling it well enough that i shouldn't bother with the delay()?
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

there is input()
but thats only if you hit enter

try this:

Code: Select all

stop=0
repeat
  if inkey()=#PB_Key_Any
    stop=1
  endif
until stop=1
i didn't cheak it or anything but you get the idea
I know for certain that there is a build in constant for "any key"
~Dreglor
Proteus
Enthusiast
Enthusiast
Posts: 113
Joined: Wed Sep 17, 2003 8:04 pm
Location: The Netherlands

Post by Proteus »

Dreglor wrote:there is input()
but thats only if you hit enter

try this:

Code: Select all

stop=0
repeat
  if inkey()=#PB_Key_Any
    stop=1
  endif
until stop=1
i didn't cheak it or anything but you get the idea
I know for certain that there is a build in constant for "any key"
Yes, but the constant is a numerical value (I DID test it :) ).

Code: Select all

This works:
stop=0
repeat
  if inkey()<>""
    stop=1
  endif
until stop=1
But that's basicly the same as what Blueznl put in his first post...

A loop like this doesn't really seem to cost any computing power, but that could be because of my P4. :D
P4 2.4GHz, 256 MB, WinXP Pro, onboard video&audio.
The Programmer's Drinking Song:
"99 little bugs in the code,
99 little bugs.
Fix one bug, recompile
100 little bugs in the code."
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i'm just anal retentive when it comes to loops :)

it probably doesn't matter much on winxp / nt / 2k these days, but i just hate the feeling of loosing computing power doing nothing :)
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Proteus wrote:
Dreglor wrote:there is input()
but thats only if you hit enter

try this:

Code: Select all

stop=0
repeat
  if inkey()=#PB_Key_Any
    stop=1
  endif
until stop=1
i didn't cheak it or anything but you get the idea
I know for certain that there is a build in constant for "any key"
Yes, but the constant is a numerical value (I DID test it :) ).

Code: Select all

This works:
stop=0
repeat
  if inkey()<>""
    stop=1
  endif
until stop=1
But that's basicly the same as what Blueznl put in his first post...

A loop like this doesn't really seem to cost any computing power, but that could be because of my P4. :D
Smack a Delay() in there as well, and it will hog less resources..
Like this:

Code: Select all

stop=0
repeat
  if inkey()<>""
    stop=1
  endif
  Delay(2)
until stop=1

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Proteus
Enthusiast
Enthusiast
Posts: 113
Joined: Wed Sep 17, 2003 8:04 pm
Location: The Netherlands

Post by Proteus »

Yeah, I could've done that, but the process had used about 0 seconds of CPU-time after a minute (according to the window that pops up on XP when you press CRTL-ALT-DELETE), so I figured it wasn't necessary.
P4 2.4GHz, 256 MB, WinXP Pro, onboard video&audio.
The Programmer's Drinking Song:
"99 little bugs in the code,
99 little bugs.
Fix one bug, recompile
100 little bugs in the code."
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

well i guess my first guess was right then, thx guys
Post Reply