Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 01:45 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : keyboard keys pressed

Author Message
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2127
Posted: 10:47pm 20 Mar 2024
Copy link to clipboard 
Print this post

hi. a prob reading the key pressed on vga usb keyboard.
mode 1
cls
do
k = asc(inkey$)
print k
loop

this prints lots of 0's but not many keypresses even holding the key down.

this code responds to keys and changes which sprite to use for flame animation but remembers last key so if no keys then uses no flame sprite but no key happens all the time even if a key is pressed. do I set auto repeat? no idea.

do
k = asc(inkey$)
'if kthen
select  case k
Case 100 'd up
spnum=2
gravity=gravity-.35
Case 115 's left
spnum=5
inertia=inertia-0.05
Case 102 'f right
spnum=6
inertia=inertia+0.05
case 83  'shift s up/right
spnum=3
inertia=inertia+0.05
gravity=gravity-.35
case 70  'shift f up/left
spnum=4
inertia=inertia-0.05
gravity=gravity-.35
case 0
spnum=1
gravity=0.15
end select
'else
'spnum=1
'gravity=0.15
'endif
last_lander_x=lander_x:last_lander_y=lander_y
lander_y=lander_y+gravity-antigravity
lander_x=lander_x+inertia
'
inc fnc: if fnc=20 then fnc=0:fno=not fno'which lander for animation
box last_lander_x,last_lander_y,30,39,,0,0 'erase lander
if fno=0 then'draw noflame lander
sprite WRITE 1,lander_x,lander_y
else'draw flame lander
sprite WRITE spnum,lander_x,lander_y
end if

loop' later
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 11:35pm 20 Mar 2024
Copy link to clipboard 
Print this post

Perhaps experiment with this.
  Quote  PicoMite User Manual Page 117
ON KEY target
The first version of the command sets an interrupt which will call 'target' user
defined subroutine whenever there is one or more characters waiting in the serial
console input buffer.

Note that all characters waiting in the input buffer should be read in the interrupt
subroutine otherwise another interrupt will be automatically generated as soon as
the program returns from the interrupt.


or
ON KEY ASCIIcode, target
The second version allows you to associate an interrupt routine with a specific
key press. This operates at a low level for the serial console and if activated the
key does not get put into the input buffer but merely triggers the interrupt. It uses
a separate interrupt from the simple ON KEY command so can be used at the
same time if required.

In both variants, to disable the interrupt use numeric zero for the target, i.e.:
ON KEY 0. or ON KEY ASCIIcode, 0

Processing the key is done in a subroutine.
In the event of multiple key presses clear the keyboard buffer before returning to the main program.
Something like this at the end of the Sub might work:-
Do : Loop Until Inkey$ = ""
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2127
Posted: 12:00am 21 Mar 2024
Copy link to clipboard 
Print this post

thanks phil. do[n my brain, knew it wouldn't be as easy as inkey$ but the logic seemed to make sense but no keys is the main output values.
I'll go back to my mcp 23017 i2c expander board with 8 buttons. sod compatibility sharing code.
no, I'll try interrupts, they can't be difficult.
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 02:52am 21 Mar 2024
Copy link to clipboard 
Print this post

Maybe InKey$ can do what you want if you ignore k = 0, ie change Case 0 to Case Else for any key without a Case. See if this is useful.
do
 k = asc(inkey$)
 if k then
  select  case k
  Case 100 'd up
  spnum=2
  gravity=gravity-.35
  spnum=5
  inertia=inertia-0.05

  '''
  case else
  spnum=1
  gravity=0.15
  end select
 endif
 ...

loop

Edited 2024-03-21 13:04 by phil99
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9114
Posted: 07:03am 21 Mar 2024
Copy link to clipboard 
Print this post

Pico USB versions support the keydown function - see the CMM2 manual for details
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2127
Posted: 08:20pm 21 Mar 2024
Copy link to clipboard 
Print this post

This doesn't show a key pressed in a prog.
ON KEY 100,d_up
ON KEY 115,s_left
ON KEY 102,f_right
ON KEY 83,S_up_left
ON KEY 70,F_up_right


sub d_up
spnum=2
gravity=gravity-.35
end sub
'
sub s_left
spnum=5
inertia=inertia-0.05
Do : Loop Until Inkey$ = ""
end sub
'
sub f_right
spnum=6
inertia=inertia+0.05
Do : Loop Until Inkey$ = ""
end sub
'
sub F_up_right
spnum=3
inertia=inertia+0.05
gravity=gravity-.35
Do : Loop Until Inkey$ = ""
end sub
'
sub S_up_left
spnum=4
inertia=inertia-0.05
gravity=gravity-.35
Do : Loop Until Inkey$ = ""
end sub
'

neither did this except keydown(1)=100 which seemed to be stuck or repeating
if keydown(1)=100 then spnum=2:gravity=gravity-.35
else if keydown(1)=115 then spnum=5:inertia=inertia-0.05
else if keydown(1)=102 then spnum=6:inertia=inertia+0.05
else if keydown(1)=83 then spnum=3:inertia=inertia+0.05:gravity=gravity-.35
else if keydown(1)=70 then spnum=4:inertia=inertia-0.05:gravity=gravity-.35
else if keydown(1)=0 then spnum=1:gravity=0.15
end if
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024