PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

OnKeyDown+Pause

Hi!
I'm trying to make my text slide down slowly while key is pressed down.

Code:
HRESULT InitialApplication::onKeyDown(DWORD dwKey, KeyList& keylist)
{
while ((textpos > textposd) && (keylist.vkDown == dwKey))
{
textupanddown = textupanddown - 15;
textpos = textpos - 1;
pause(150);
}
while ((textpos < 0) && (keylist.vkUp == dwKey))
{
textupanddown = textupanddown + 15;
textpos = textpos + 1;
pause(150);
}

return ReturnCode::OK;
}

As You can see, I'm using *pause* to stop text from immidiate scrolling from top to the very bottom, but it still just freezes whole screen for a sec. and then all I see is bottom of the text. Probably I need some original solution, because as I know from GledDocs *pause* function works a bit differently from what I need, so I'm really looking forward for help!
Hi Ryumaster,

The onKeyDown method will be executed only once when the key is pressed, and the onNextFrame method (which refreshes the screen) won't be called until onKeyDown finishes.
What you could do, is to enable/disable some flags in the onKeyDown/onKeyUp methods, and update the text position in onNextFrame.


Code:

enum { KEYISUP = 1,
KEYISDOWN = 2
};

DWORD keyFlag = 0;

HRESULT InitialApplication::onKeyDown(DWORD dwKey, KeyList& keylist)
{
if(dwKey == keylist.vkUp)
keyFlag |= KEYISUP;
if(dwKey == keylist.vkDown)
keyFlag |= KEYISDOWN;

return ReturnCode::OK;
}

HRESULT InitialApplication::onKeyUp(DWORD dwKey, KeyList& keylist)
{
if(dwKey == keylist.vkUp)
keyFlag &= ~KEYISUP;
if(dwKey == keylist.vkDown)
keyFlag &= ~KEYISDOWN;

return ReturnCode::OK;
}


And then in onNextFrame you can do:

Code:
if(keyFlag & KEYISUP){
//move text up a little
}
if(keyFlag & KEYISDOWN){
//move text down a little
}


Regards,
Sergio.
Attachments:
Application.gif
Nomad · 02 Nov 2006, 18:36 UTC — You need to understand fundamentals of event handling
Hi, Ryumaster!

OnKey(Down/Up) event is fired when some of keys is (pressed/released). When this event occur the onKeyDown()/onKeyUp() is called. And whole program is waiting while onKeyDown()/onKeyUp() returns. So no hard cycles or pauses is allowed if you want to see your result.

All timed work do in onNextFrame() but again no cycles and pauses if you want to get as maximum fps as possible.

For your problem I can suggest such scenario.
Declare bool variables in your class for every button you want to process.
In onKeyDown()/onKeyUp() set this variables to true/false if appropriate button pressed/released
And in onNextFrame() check this flags and make needed reactions.

If you need to syncronize your processing to make them independent from number of FPS, measure time passed between every call of onNextFrame().
Hi! Tnx a lot, Sergio, now I fully understand how to make it work.

Nomad, спасибо! Идея хорошая, и сихронизация вродь пока не особо нужна, так что попробовать стоит :)