It is currently Tue Jul 23, 2019 1:54 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Apr 04, 2019 7:03 am 
Offline
Formerly Fx3
User avatar

Joined: Fri Nov 12, 2004 4:59 pm
Posts: 3187
Location: Brazil
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:
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.


Top
 Profile  
 
PostPosted: Thu Apr 04, 2019 7:55 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21511
Location: NE Indiana, USA (NTSC)
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?

_________________
Pin Eight | Twitter | GitHub | Patreon


Top
 Profile  
 
PostPosted: Thu Apr 04, 2019 6:55 pm 
Offline
Formerly Fx3
User avatar

Joined: Fri Nov 12, 2004 4:59 pm
Posts: 3187
Location: Brazil
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.

Quote:
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..?


Top
 Profile  
 
PostPosted: Fri Apr 05, 2019 12:54 pm 
Offline

Joined: Sun Sep 19, 2004 11:07 pm
Posts: 172
That should probably look more like

Code:
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...


Top
 Profile  
 
PostPosted: Sat Apr 06, 2019 12:15 pm 
Offline
User avatar

Joined: Fri Nov 19, 2004 7:35 pm
Posts: 4192
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!


Top
 Profile  
 
PostPosted: Sat Apr 06, 2019 7:54 pm 
Offline
Formerly Fx3
User avatar

Joined: Fri Nov 12, 2004 4:59 pm
Posts: 3187
Location: Brazil
Ok, thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: Ice Man and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group