keypressed event

copiloto

Member
Licensed User
Longtime User
Hello,

I'd like to know if somebody has a solution for this question: Example:

Sub Timer1_tick
if hk.keypressed=37 then counter=counter+1 '(counter is a general variable)
hk.keypressed=38
End Sub

If you keep pressed left hardkey, although timer1.interval is too short, maximum speed of keypressed event is too slow. I have calculated it and is around 15 catched events in my Pocket (400 Mhz). So, if you try to implement an experimental game, you must make big increments of pixels to simulate advance of a palette (for example), and it doesn't look great..

Someone knows how to implement a quicker way to detect if the hardkey button is pressed? For now, I've only a little trick:
after keypressed is catched, assign a value to a variable. If you press central key, it stop, but it's not a nice solution...

Thanks in advance.
 

Cableguy

Expert
Licensed User
Longtime User
SOmetimes the device "locks-up", while doing something heavy like GUI changes, and therefore some events, seem not to be fired, or take too long to fire...
Some of this behaviour is somewhat surpassed by using the DoEvents keyword, wich forces the device to halt what-ever it was doing, and "pay attencion" to the events being fired.
 

copiloto

Member
Licensed User
Longtime User
Hi Cableguy,

Thank you very much for your response.

I've tried to introduce "doevents" into Timer1 routine, but the program gets a "StackOverflow Exception". It may appears because of short time of timer: 2 ms...

These are the two versions with summary at the end:

Program1
------------

Sub Globals
counter=0
End Sub

Sub App_Start
h.New1("Form1",false,false,true)
Form1.Show
timer1.Interval=2
timer1.Enabled=true
End Sub

Sub Timer1_Tick
if h.keypressed=37 then counter=counter+1
label1.text=counter
h.keypressed=38 ('if user remove finger from left hardkey, keypressed shouldn't be 37)
End Sub


Program2
------------

Sub Globals
counter=0
End Sub

Sub App_Start
h.New1("Form1",false,false,true)
Form1.Show
timer1.Interval=2
timer1.Enabled=true
End Sub

Sub Timer1_Tick
if h.keypressed=37 then counter=counter+1
label1.text=counter
End Sub

-------------------------------------------------------------

There is a great different performance in two programs:
Program1 counter is about 10-15 per second
Program2 counter is about 120-150 per second (x10)

The great problem is how to accelerate the catching event property, I mean,
how to accelerate first program, complying with requirement:
('if user remove finger from left hardkey, keypressed shouldn't be 37)

First program (counter value) will never be as quick as second, but, could it be 50% at least?

Thanks in advance.
 

Cableguy

Expert
Licensed User
Longtime User
From my experience, even on desktop, an interval less than 100, is not doable...
If you take a look at the keyboard props on you desktop, even them have a minimum of 14ms delay between reps.
But a stak over flow, means that you have somewere a loop that has some memmory leak...
Perhaps it is the background image, or loaded imges that are causing the memory to fill-up...
It all deppends on what and how you are doing things.
 

agraham

Expert
Licensed User
Longtime User
Don't use DoEvents in a Timer event Sub. Timers are called from the application message loop. DoEvents re-enters the message loop to process messages and and cause your Timer event Sub to be re-entered if the Timer intervals are too short. If DoEvents is then called you get another re-entry and your call stack blows away.

If you really really must call DoEvents in a Timer events Sub protect yourself from re-entry
B4X:
InTimer = false; ' global variable

...

Sub Time_Tick
  If InTimer Then
    Return
  Else
    InTimer = True
    ...
    DoEvents
    ...
    InTimer = False
End Sub
 

copiloto

Member
Licensed User
Longtime User
Cableguy, thanks for sharing your experience and statistics about events. I'll have to redirect my programs to another way of working.

Agraham, thanks for your recommendations and explanations about using Timer.

Best regards!
 
Top