Prevent Chopping Key

Started by deetee, November 12, 2010, 02:43:23 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

deetee

Hi all!

Much earlier as assumed I have a further question regarding this code:

iskeeboard=0;
repeat
  if(key(_INS)) iskeeboard=-iskeeboard+1; end

  if(iskeeboard)
   show_keyboard();
  end
...
until(escape);


If a switch (iskeeboard) equals 1 a keyboard should be shown. iskeeboard could be toggled between 0 and 1 with the _INS key.

Unfortunately bennu is so fast ( :)) that iskeeboard switches many times and remains "random". What can I do to switch iskeeboard only one time if _INS is pressed one time.

Thanks for any idea.
deetee

handsource-dyko

When a key responds too fast, I recommend using a frame statement with a number bewteen the brackets.
I ofter experiment with the number, but anything between 100 and 1000 should be fine.

O, you should NOT write: IF (iskeyboard) like this. It is unclear, because this is always true, no matter the value.

It's formally better to write: IF (iskeyboard==1)otherwise it's a pointless test right?
I know I might sound annoying, but I think that's bad coding style, because it's confusing, and may be a source of bugs that
can easily be overlooked. It just looks odd to me.

But anyway, try
       


   byte iskeyboard=FALSE; 

   REPEAT

        // check if the Insert key was pressed, and delay the keypress
        // to prevent a too quick response.
        IF (key(_INS) AND iskeyboard==FALSE)
            FRAME(100);
            iskeyboard=TRUE;
        END

        // when the Insert key has been pressed, execute the show_keyboard routine.
        IF (iskeyboard==TRUE)
            show_keyboard();
        END

   FRAME;
   UNTIL (key(_esc)) // note, this check is ok, because the key function acutally returns a value.

EugeneP

In such cases I just wait until the key is released.

[code language="bennu"]   
if( jkey( _INS )  )
    while( jkey( _INS) ) frame; end
    iskeyboard = ! iskeyboard;
end
IF ( iskeyboard )
    show_keyboard();
END
[/code]

deetee

Great - thanks for the fast reply and the good solutions.
I will test, which solution (or combination) is the best for my purpose.

Regards
deetee

Windgate

There is a .lib with a FUNCTION called _key that let you read only the key_down or the key_release, Splinter made it some months ago.
Iván García Subero. Programador, profesor de informática, monitor de actividades culturales y presidente de TRINIT Asociación de Informáticos de Zaragoza. http://trinit.es

Sandman

Quote from: handsource-dyko on November 12, 2010, 04:36:10 PM
O, you should NOT write: IF (iskeyboard) like this. It is unclear, because this is always true, no matter the value.

It's formally better to write: IF (iskeyboard==1)otherwise it's a pointless test right?

In DIV and old Fenix versions, an integer was considered 'true' if it was odd and 'false' it it was even. Not coincidentally, process ID's were always an odd number. In newer Fenix versions and in Bennu, an integer value is considered 'true' if it is NOT 0 and 'false' if it IS 0. The reason for this change is that it is much more widely used. There are also languages, like Java, that do not accept integers where a boolean is expected. In such languages, one would need to write if(a!=0) to test if the integer a is 'true' (note that the 'true' in this sentence bears the Bennu/C/C++/etc meaning).

So in Bennu, there is absolutely nothing wrong with if(<value>). If you absolutely want to make it appear more correct, use if(a==true) or if(a!=false) instead of if(a==1).

Those key_down and key_release functions are a good addition. Hope to see them on the wiki. :)
-- Sandman

josebita

Nice to see you around again, Sandman.
I'll try to put them on the wiki tomorrow.