HP Forums

Full Version: HP39GII GETKEY command
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I am struggling with the HP39GII GETKEY command.

This crude bit of code works under the emulator, although with a few glitches and false returns:

EXPORT TRY1()
BEGIN
0->A;
REPEAT
RECT();
GETKEY->A;
TEXTOUT_P(String(A),10,10);
WAIT(1);
UNTIL A=5;
END;

i.e. it reads and displays key press codes until key 5 (F6) is pressed.

-1 is displayed when no key is depressed.

When I USB it across to the actual HP39GII, it doesn't work at all. Zero is returned whatever the state of the keyboard.

I tried a similar program on my HP39GS and this behaved as expected. On this earlier model, a call of GETKEY actually suspends processing until a key is pressed. On the HP39GII it just runs through but throws up the code number for the last key pressed.

Has anybody else successfully used GETKEY on their hardware HP39GII?


Edited: 11 Aug 2012, 10:58 a.m.

Thanks for the information. Will investigate. Send me an email at timwessman at hp.com.

I believe the change to not pause was made since it seemed most of the time people wanted to use this is a loop and continue processing (for example, a game or moving an object). Pausing meant that was pretty much impossible. We could probably investigate having an optional "time" value from 0 on up that would specific the amount of wait time. Thoughts?

TW

The 50G is fine for this...

1/ Wait Command: Suspends program execution for specified time, or until a key is pressed.
The function of WAIT depends on the argument, as follows:
• Argument x interrupts program execution for x seconds.
• Argument 0 suspends program execution until a valid key is pressed. WAIT then
returns xkey, which defines where the pressed key is on the keyboard, and resumes program execution.

(see AUR)

If i'm not wrong, i remember also that the 0 WAIT on the 50G use very few power (on the the contrary of a loop with a key test)

On my 39gII Rev 16633 GETKEY don't work
On the emulator (Rev 17565), the result seems curious (not always the same number for the same key !)

EXPORT Test
BEGIN
LOCAL a;
REPEAT
a:=GETKEY;
IF a<>-1 THEN RECT(); TEXTOUT_P(string(a),10,10); END;
UNTIL a==5;
END;

Tim, a question : why many functions are in CAPS but few not (for example why 'string' and not 'STRING' ?

Edited: 11 Aug 2012, 7:15 p.m.

Hi Charles,

Please state the version/date of your emulator and rom.

A bug in GETKEY have been reported (by me) in beta-test forum some time ago, and as far as I know, the correction is done, but I am not sure it makes its way to public releases already.

As for GETKEY not suspending execution, it is a behavior which match the manual. I don't like it as any program which use it will drain the batteries while waiting for the keyboard.

Patrice

Generally, it means they came from XCAS, or started in XCAS and then later a strictly numeric version was made.

TW

Version string on the emulator is 2012 07 10 17565.

It behaves as Gilles reports in his post above.

The physical calculator's ROM is 09/May/2012 Revision 16633.

Edited: 12 Aug 2012, 1:28 a.m.

This should explain.

The bug was here in May and was corrected before your emulator release.

Check for ROM update.

I will re-flash as soon as a newer ROM download becomes available.

I have a work-around for the "GETKEY" problem that uses the "ISKEYDOWN" function, which works properly. Code like this could be re-jigged as a function in any given program:

EXPORT TRY1()
BEGIN
REPEAT
0->A;
-1->B;
REPEAT
B+1->B;
ISKEYDOWN(B)->A;
UNTIL A=1 OR B=50;
RECT();
IF A=1 THEN
TEXTOUT_P(string(B),10,10);
END;
UNTIL B=0;
END;

Keys are polled sequentially until one is found to be down or key 50 has been reached. If a key was down its id is reported. Key 0 (F1) exits the program.

This works like the wait-until-a-key-is-pressed style "GETKEY" command on the HP39GS.

Hi Charles

Interesting. It works fine. I think there is differences between your program and GETKEY :

- if you call 2 times TRY1 and a key is pushed (and not released) , it will returs 2 times the code key. I imagine GETKEY would only returns the key for the first call (if you don't release the key and push again)as it uses the keyboard buffer.

- The buffer is not cleared with ISKEYDOWN (you can can clear it with a call of GETKEY if ISKEYDOWN)

To manage the keyboard, we need 3 functions :

1/ GETKEY (to read the last key in the buffer)

2/ ISKEYDOWN (to scan the keyboard in real time)- Perfect for games ;)

3/ WAIT(0) like the 50G : stop the program, and wait until a key is down and then return the key number - low power consumption)

PS : I've not tried yet, but it will be interesting to see if ISKEYDOWN can manage multiple keys down in the same time

EDIT : Yes you can, with the real calc (not with 17565 rev emulator (perhaps because of the PC keyboard ? You can't push 2 keys only with the mouse ;) ) :

 EXPORT MyFirstGame
BEGIN
LOCAL k;
REPEAT
k:="";
IF ISKEYDOWN(10) THEN k:=k+" Right"; END;
IF ISKEYDOWN(14) THEN k:=k+" Left"; END;
IF ISKEYDOWN(9) THEN k:=k+" Up"; END;
IF ISKEYDOWN(15) THEN k:=k+" Down"; END;
IF ISKEYDOWN(6) THEN k:=k+" FIRE"; END;
GETKEY;RECT(); TEXTOUT_P(k,10,10);WAIT(0.05);
UNTIL 0;
END;

So you can handle FIRE and Left/Right keys pressed in the same time

Another example :

EXPORT MyFirstGame
BEGIN
LOCAL k,m,t,n;
k:={
{32," seven "},{33," eight "},{34," nine "},
{37," four "} ,{38," five "} ,{39," six "},
{42," one "} ,{43," two "} ,{44," three "},
}
REPEAT
t:="";
FOR n FROM 1 TO 9 DO
m:=k(n);
IF ISKEYDOWN(m(1)) THEN t:=t+m(2); END;
END;
RECT(); TEXTOUT_P(t,10,10);
WAIT(0.05);
UNTIL 0;
END;

Is it possible to access to one item of a sublist to avoid use of temporary variable (m in this exemple) ?

For example here :(i want to get "nine")

k(3,2) -> Number of argument error

k(3)(2) -> k(3)*(2)

The only way i have found is : m:=k(3); ....m(2)...


Edited: 12 Aug 2012, 9:41 a.m.