Annoyance with Allegro + key polling

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Post Reply
User avatar
Zepper
Formerly Fx3
Posts: 3194
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Annoyance with Allegro + key polling

Post by Zepper » Thu Apr 04, 2019 7:03 am

Hi.

I'm having an annoyance in my code. Such problem wasn't present in the previous version of my program.
Basically... Allegro has a volatile char key[MAX_KEYS] array that holds the state of each keyboard key, as 0 (released) or -1 (pressed). More info here.

In order to avoid a deadlock on checking keys, I usually do...

Code: Select all

unsigned char value=0;
char my_key=0;
if( key[KEY_T] )
{
   value |= 0x40;
   if(0 == my_key) do_stuff(); //called only once! Avoids multiple calls while the key is pressed.
} 
my_key = key[KEY_T];
However, for some bizarre reason, the code doesn't work as it should do because my_key is ALWAYS zero.

tepples
Posts: 22018
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Annoyance with Allegro + key polling

Post by tepples » Thu Apr 04, 2019 7:55 am

In what scope does char my_key=0; execute? Global or within a function? This will help me figure out whether it's a static or automatic variable.

Are you calling poll_keyboard()? What does keyboard_needs_poll() return?

User avatar
Zepper
Formerly Fx3
Posts: 3194
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Annoyance with Allegro + key polling

Post by Zepper » Thu Apr 04, 2019 6:55 pm

tepples wrote:In what scope does char my_key=0; execute? Global or within a function? This will help me figure out whether it's a static or automatic variable.
It's static and set to zero on emulator launch only.
Are you calling poll_keyboard()? What does keyboard_needs_poll() return?
Not calling it.
keyboard_needs_poll() returns 0.

Perhaps some compiler flag is changing the behaviour..?

ReaperSMS
Posts: 174
Joined: Sun Sep 19, 2004 11:07 pm
Contact:

Re: Annoyance with Allegro + key polling

Post by ReaperSMS » Fri Apr 05, 2019 12:54 pm

That should probably look more like

Code: Select all

char kval = key[KEY_T];
if (kval)
{
   value |= 0x40;
   if (0 == my_key) do_stuff();
}
my_key = kval;
What could be happening is that KEY_T gets released during do_stuff().

This is of course presuming you've called the setup code needed to get the timer and keyboard running, and haven't been messing around with the keyboard callback stuff. The callbacks may be a better approach for modern systems, interrupt driven keyboard stuff hasn't been relevant to user applications since dos died out...

User avatar
Dwedit
Posts: 4333
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Annoyance with Allegro + key polling

Post by Dwedit » Sat Apr 06, 2019 12:15 pm

On Win32, you can do keyboard input one of two ways:
* Monitor the WM_KEYUP/WM_KEYDOWN messages (occasionally you miss the KEY UP and get stuck keys)
* Call GetKeyState

So whatever Allegro does will ultimately turn into one of these two for Win32 programs.

If you aren't calling the message loop, you won't see any KeyUp/KeyDown messages, and if there are no calls to GetKeyState, you're just reading some bytes from memory. There would need to be a secondary thread changing that memory in order for something like that to work.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
Zepper
Formerly Fx3
Posts: 3194
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Annoyance with Allegro + key polling

Post by Zepper » Sat Apr 06, 2019 7:54 pm

Ok, thanks.

Post Reply