TRSE: What am I doing wrong with keypressed?

You need an actual VIC.

Moderator: Moderators

Post Reply
JavaJack
Vic 20 Drifter
Posts: 39
Joined: Fri May 30, 2014 10:19 am

TRSE: What am I doing wrong with keypressed?

Post by JavaJack »

I'm brand new to TRSE.

I want to fill the screen with garbage, until space is pressed, which should exit the program.

What actually happens is that the program only performs a single trip through the while loop then exits immediately.

Code: Select all

program MyProgram;
var  
   i: integer;
   c: byte = 65;
   k: byte = 0;

begin
	definescreen();
	clearscreen(32, screen_char_loc);	
	clearscreen(BLUE, screen_col_loc);
	
	while k = 0 do
	begin
		screenmemory := screen_char_loc;
		i := 0;
		while i < 506 do
		begin
			screenmemory[0] := c;
			screenmemory := screenmemory + 1;
			i := i + 1;
			c := c + 1;
		end;
		if (keypressed(KEY_SPACE) = 1) then k := 1;
	end;
end.
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: TRSE: What am I doing wrong with keypressed?

Post by Mike »

What happens if you leave out the line: :?:

Code: Select all

if (keypressed(KEY_SPACE) = 1) then k := 1;
Then, at no place the variable k will set to a value different from 0, and the program should remain in an infinite loop.

If that results in the desired infinite loop, you should check the documentation regarding the use of the "keypressed()" function.

If you otherwise still get only one execution of the main loop, you should file a bug report with the developers.

Greetings,

Michael
JavaJack
Vic 20 Drifter
Posts: 39
Joined: Fri May 30, 2014 10:19 am

Re: TRSE: What am I doing wrong with keypressed?

Post by JavaJack »

What happens if you leave out the line:
Yeah, if I comment out the "keypressed" line, I get an infinite loop.
check the documentation regarding the use of the "keypressed()" function
Sure, seems straightforward.

Code: Select all

Description 
Tests whether the key specified in parameter 1 is presser or not. Returns 1 if true, 0 if not. 
Example 
 if ( keypressed( KEY_R ) = 1 ) then ...
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: TRSE: What am I doing wrong with keypressed?

Post by Mike »

The page https://github.com/leuat/TRSE/blob/mast ... ressed.rtf tells:
See CONSTANTS section in help for a list of KEY_ values available to use.
That page however: https://github.com/leuat/TRSE/blob/mast ... stants.rtf is completely silent on any information about the parameter range for keypressed(). Go figure. :?
JavaJack
Vic 20 Drifter
Posts: 39
Joined: Fri May 30, 2014 10:19 am

Re: TRSE: What am I doing wrong with keypressed?

Post by JavaJack »

I upgraded to 10.6 but still no dice.
User avatar
AndyH
Vic 20 Afficionado
Posts: 364
Joined: Thu Jun 17, 2004 5:51 am
Website: https://www.hewco.uk
Location: UK
Occupation: Developer

Re: TRSE: What am I doing wrong with keypressed?

Post by AndyH »

Try this:

Code: Select all

if ( getkey() = KEY_SPACE ) then
--
AndyH
HEWCO | Vic 20 blog
JavaJack
Vic 20 Drifter
Posts: 39
Joined: Fri May 30, 2014 10:19 am

Re: TRSE: What am I doing wrong with keypressed?

Post by JavaJack »

That works. With the caveat that all keys pressed, up to, and including the space, are printed upon exit, below the READY prompt. Not a big deal for a throwaway test program like this.

Is getkey() documented somewhere and I'm just blind?
User avatar
AndyH
Vic 20 Afficionado
Posts: 364
Joined: Thu Jun 17, 2004 5:51 am
Website: https://www.hewco.uk
Location: UK
Occupation: Developer

Re: TRSE: What am I doing wrong with keypressed?

Post by AndyH »

Sorry for the late response.

Yes GETKEY() is in the help accessed via the F1 key. Press F1 while the cursor is on the word GETKEY() in the editor to jump directly to it, or to find it in help look in the Methods category and geykey will be in the list. You will also find in the Constants category a list of contant names beginning with KEY_ that you can use with it.

However, Getkey is a bit simple and has the limitation similar to reading a key from BASIC in that it only returns one key press value. Press two or more keys at the same time and only one of them will win out.

I'll hold my hands up here that this implementation is a fault on my part due to what I knew / learnt at the time of implementing a getkey() function in TRSE (for the Vic 20) and also that I've not had a requirement to detect more than one keypress before (as my first two games expected joystick controls). Generally, TRSE is improved by people using it and requesting functionality and for a time there was only me supplying requirements and fulfilling them ;) Thankfully, there has been more interest from Vic 20 programmers using TRSE in recent weeks which is keeping me busy.

I will add some better support for the keyboard in TRSE programs in the future, but until then if you want to test if two or more keys are pressed at the same time, this is what I have figured out. Add a bit of inline assembly to your TRSE code:

Code: Select all

asm("
  lda #127 ; put the row number in here from the table linked below
  sta $9120
  lda $9121 ; the result is bits 0-7 being 'off' (0) for each key in that row currently held down
  sta YourTRSEvariable ; store the result in a trse variable if you want to, or do your own asm here
");
Change the first line value (127) to the row you want test a key being held down on, the values are in the table below. Reading from $9121 will give you 8 bits to test, each bit represents a key from that row. For example, with row 127, you can detect keys F7, HOME, -, 0, 8, 6, 4 and 2. To see if key '2' is held, you need to check if bit 0 is clear. Repeat the above asm if there are keys on other rows you want to test. Hope that helps :)

View key mappings
--
AndyH
HEWCO | Vic 20 blog
JavaJack
Vic 20 Drifter
Posts: 39
Joined: Fri May 30, 2014 10:19 am

Re: TRSE: What am I doing wrong with keypressed?

Post by JavaJack »

Thanks :)
Post Reply