English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 09 January 2021, 06:18   #1
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,646
Key up detection?

I have hit a brick wall here over something that seems rather stupid but I cannot figure out. How should I approach detecting a key not being pressed anymore?

I feel like I have to make a state machine here, but I have not succeeded in doing so.

I would like to:
- Press key
- Blit a shape
- This shape should remain for as long as the key is pressed
- Blit back the original state when the key is depressed

I'm using Blitz mode and RawStatus. This currently either does not work (a keypress blits the shape but then it stays there), or if it works, it means after the key is depressed, all the default states get blitted back and get themselves blitted every frame (unoptimal and wrong, this will reset ay other shapes i will have in the future)


Any help would be appreciated.
Amiga1992 is offline  
Old 09 January 2021, 12:09   #2
Master484
Registered User
 
Master484's Avatar
 
Join Date: Nov 2015
Location: Vaasa, Finland
Posts: 525
Here is a code that reads the space key and detects all possible states: UP, DOWN and RELEASE.

Code:
vwait

oldstate = currentstate

key = RawStatus($40)

If key = 0 then currentstate = #UP
If key = -1 then currentstate = #DOWN

If oldstate = #DOWN AND currentstate = #UP
 currentstate = #RELEASE
End If
The #RELEASE status lasts only 1 frame.
Master484 is offline  
Old 09 January 2021, 15:07   #3
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,646
Ahhh you legend, that works quite nicely, yeah.
However, I have a BUNCH of keys here, I need to do this for each and I am not even reading the whole keyboard!

Is there any RawStatus for nothing being pressed? $0?
Amiga1992 is offline  
Old 09 January 2021, 18:30   #4
Master484
Registered User
 
Master484's Avatar
 
Join Date: Nov 2015
Location: Vaasa, Finland
Posts: 525
Quote:
Is there any RawStatus for nothing being pressed? $0?
No, I don't think that there is any easy way to detect when all keys are "up".

Maybe the easiest way is to have a variable named "AnyKeyDown", and set that to 0 before your key check routines start. And then in all of your key checks you set that variable to 1 if any of those keys were down.

Or instead of that you could maybe add together all RawStatus return values of the keys that you check, and if the result is zero, then none of them were pressed.
Master484 is offline  
Old 10 January 2021, 18:21   #5
E-Penguin
Banana
 
E-Penguin's Avatar
 
Join Date: Jul 2016
Location: Darmstadt
Posts: 1,214
Maybe you could use Inkey$ to get whatever key is pressed?
E-Penguin is offline  
Old 11 January 2021, 16:30   #6
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,646
Quote:
Originally Posted by Master484 View Post
No, I don't think that there is any easy way to detect when all keys are "up".
Hmm I htought so, also, you provide some more good ideas. Thanks a lot. i'm gonna see what I do today.

As it is now, I have a procedure that I call, where I check RawStatus for all the keys I am supposed to be reading. I wonder if I can optimize that but for now it should be OK.

Also, I have some shift-key commands, am I correct in nesting these readings , starting with the shift key? Should I use "While"? I thought that'd freeze my program in the procedure so I just used Ifs.

Quote:
Originally Posted by E-Penguin View Post
Maybe you could use Inkey$ to get whatever key is pressed?
I understood Inkey$ requires an enter after typing. It's for reading string input. Not what I want.
Amiga1992 is offline  
Old 11 January 2021, 23:46   #7
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,646
Quote:
Originally Posted by Master484 View Post
Or instead of that you could maybe add together all RawStatus return values of the keys that you check, and if the result is zero, then none of them were pressed.
This worked beautifully man. I did it this way.

Code:
Function.s WhichKey {}
	result$ = ""
	counter.b = 0
	
	If RawStatus($20)
		result$ = "A"
		counter+1
	End If
	If RawStatus($21)
		result$ = "S"
		counter+1
	End If
	If counter = 0
		result$ =  "nothing"
	End If
	Function Return result$
End Function
"counter" is only 0 if no key was pressed.
Thanks so much!

Now I need to tackle the key hold problem, but I might do this in a completely different way, so I'm gonna wait a little bit until I work on other things.


[edit] hmmm this keyboard code is a bit of a mess, especially if I introduce shift keys. Will need more looking into.

Last edited by Amiga1992; 12 January 2021 at 01:34.
Amiga1992 is offline  
Old 12 January 2021, 16:10   #8
Master484
Registered User
 
Master484's Avatar
 
Join Date: Nov 2015
Location: Vaasa, Finland
Posts: 525
Quote:
Also, I have some shift-key commands, am I correct in nesting these readings , starting with the shift key? Should I use "While"? I thought that'd freeze my program in the procedure so I just used Ifs.
There could be an easy way to check the Shift key status in the current code that you have, and that is to add something like this after the normal key reads:

Code:
If RawStatus(Shift rawkey code)
 result$ + "SHIFT"
End If
So if only "A" was pressed, the result is just "A", but if shift was also pressed, then the result is "ASHIFT".

But if that doesn't work, then the standard way how I would handle it would be to have two routines; one checks the keys without Shift, and the other one with Shift. So the shift status is checked only once before the two routines, and depending on it's status one of the routines is selected.
Master484 is offline  
Old 12 January 2021, 16:57   #9
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,646
Quote:
Originally Posted by Master484 View Post
But if that doesn't work, then the standard way how I would handle it would be to have two routines; one checks the keys without Shift, and the other one with Shift. So the shift status is checked only once before the two routines, and depending on it's status one of the routines is selected.
The way I had it works, and it's kind of that way, but in the same routine. Basically, if either Shift is pressed, it will look for specific keys, and do its thing.

Now the problem I noticed, is that sometimes, it registers also the non shifted value. Like if I am looking for Shift+A, sometimes the program, while I keep Shift pressed and mash A, will read just A. I don't know WHY this happens because it shouldn't happen, but it could be because those checks are done before the shift checks, so maybe if I put the shift checks at the top of the keyboard reading routine, it might work better.

I'm really annoyed at how Blitz really needs you to put things in certain places but no reference to this is mentioned in the docs. you just learn with trial by fire.
Amiga1992 is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
SMC detection StingRay support.WinUAE 19 12 November 2022 17:47
Amiga key=Windows key and problems there of darkhog support.WinUAE 1 31 March 2017 22:08
Collision Detection sandruzzo Coders. General 5 10 June 2016 12:50
FS-UAE 2.5.6dev Amiga Key (Windows key) pops up the start menu Marskilla support.FS-UAE 3 23 September 2014 23:18
Amiga 'HELP' key - tribute thread to the abandoned key... Paul_s Amiga scene 22 23 July 2009 12:15

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 20:14.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.10003 seconds with 15 queries