Qbasicnews.com
August 09, 2020, 07:55:24 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: read keys once then check if it's held  (Read 1911 times)
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« on: May 28, 2004, 09:48:24 AM »

Ok... first

this doesn't work
Code:
press$ = INKEY$
IF press$ = oldpress$ THEN EXIT SUB
oldpress$ = press$


any reason why?
I'm trying to get it if you hold the key down it does go running away. you have to push it down once each time.
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Z!re
*/-\*
*****
Posts: 4599


« Reply #1 on: May 28, 2004, 09:50:35 AM »

Code:
press$=inkey$
if press$ then
 if press$=oldpress$ then exit sub
 oldpress$=press$
end if
Logged
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #2 on: May 28, 2004, 09:55:53 AM »

close... but i want it so if you push it down again it moves once but if you hold it down it moves once
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Z!re
*/-\*
*****
Posts: 4599


« Reply #3 on: May 28, 2004, 10:08:03 AM »

It can't be done, not by using Inkey$ atlest.

Code:
CLS


DO
press$ = INKEY$

IF press$ = "a" THEN a& = a& + 1 ELSE b& = b& + 1

LOCATE 1, 1: PRINT a&; b&;

LOOP


Try that, as you can see, b& always increases, even if you hold a.
So there is no good way of telling if the key is being pressed or not as Inkey$ returns chr0 anyways (in betveen the rate of repetition set in bios/windows)
Logged
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #4 on: May 28, 2004, 10:15:34 AM »

no if you add this
Code:
t = TIMER
DO: LOOP UNTIL TIMER - t > .01



anyways....



preedit: i just added the timer and now it works like i want it to?
Code:
t = TIMER
DO: LOOP UNTIL TIMER - t > .01

press$ = INKEY$
IF press$ = oldpress$ THEN EXIT SUB
oldpress$ = press$
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #5 on: May 28, 2004, 10:33:01 AM »

I've always done it with flags, one per direction. If key pressed: If flag not set move, set flag. If key not pressed: unset flag.

This works better with a custom keyboard handler, of course.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Neo
Na_th_an
*****
Posts: 2150



« Reply #6 on: May 28, 2004, 11:17:11 AM »

You could also check out INP(&H60), it already has some kind of flag system in it. Check this learning example:
Code:
DO
   Scan% = INP(&H60)
   IF Scan% <= 127 THEN PRINT "Key"; Scan%; " is being pressed" ELSE PRINT "Key"; Scan% - 128;" is being released"
LOOP UNTIL Scan% = 1
Logged
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #7 on: May 28, 2004, 05:43:07 PM »

hmmm i tried some sort of flag system didn't work though




Code:
press$ = INKEY$
IF LEN(press$) THEN
 IF press$ = oldpress$ THEN
  IF held = 0 THEN
   held = 1
  ELSE
   EXIT SUB
  END IF
 ELSE
  held = 0
 END IF
 oldpress$ = press$
END IF
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!