[AD] key_shifts bug under windows partially fixed (was: Re: Patch for wkeybd.c)

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


David A. Capello wrote:
And I found a bug with the keyboard handler of Windows.
Mainly the state of SHIFT, CTRL and ALT keys in the
global variable "key_shifts" aren't working right.

I think that the problems is because "GetKeyboardState"
is getting an old state of the keyboard (before to
process the queue of messages). So using "GetAsyncKeyState"
fix the problem. Anyway, I'm not very sure about the
cause of the bug.

I attached a simple test (bug.c) and a patch for
the src/win/wkeybd.c file which fix the problem.


Remember that key_shifts bug in Windows I reported last year ( see http://sourceforge.net/mailarchive/forum.php?thread_name=44512457.3040902%40mailbag.wackonet.net&forum_name=alleg-developers )? I tried 4.2.2 with the patch David posted, and it partially fixes the problem. The flags for SHIFT, CTRL and ALT in key_shifts now no longer get stuck as I previously reported. However, the problem with ALT-GR where pressing ALT-GR also activates the left CTRL indicator (in both key_shifts and key[]) still persists.

Attatched is the latest vertsion of the test-program I used. However, David's bug.c program also demonstrates ALT-GR activating the CTRL indicator.

Note that I've only tried 4.2.1 and 4.2.2 with the patch. Not tried 4.2.2 without the patch, but the changelog says nothing happened to the keyboard driver from 4.2.1 to 4.2.2



AE.


/***************************************************************************
 *
 * keyshifts.c
 * Test program to show the key_shifts function showing stuck shift keys in Windows
 *
 **************************************************************************/


/****************************************************************************
 Includes
 */

#include <allegro.h>

/****************************************************************************
  Types
 */

typedef int TeBool;


/****************************************************************************
 Defines
 */



TeBool
teSetupGfx()
{
	int nRetVal;

	nRetVal = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

	set_palette(desktop_palette);

	return TRUE;
}



void
teCleanupGraphics()
{

}



void
tePrintKeyPressed(TeBool bCondition, char *szText, int nX, int nY)
{
	if(bCondition)
	{
		textout_ex(screen, font, szText, nX, nY, 0, 1);
	}
	else
	{
		rectfill(screen,nX,nY,nX+text_length(font,szText),nY+text_height(font),0);
	}
}



void
teDoTest()
{
	TRACE("Starting test...\n\n");

	textprintf_ex(screen, font, 0,0, 0, 4, "Driver: %s", keyboard_driver->name);


	textout_centre_ex(screen, font, "Hold any combination of SHIFT, CONTROL, or ALT, release, and try again.", SCREEN_W/2, 20, 1, 0);



	while(!key[KEY_ESC] && !key[KEY_SPACE])
	{
		if(keyboard_needs_poll())
		{
			poll_keyboard();
		}


		/* Print results for key_shifts */
		tePrintKeyPressed(key_shifts & KB_SHIFT_FLAG, "key_shifts: SHIFT is pressed", 80, 40);
		tePrintKeyPressed(key_shifts & KB_CTRL_FLAG, "key_shifts: CONTROL is pressed", 80, 40+text_height(font));
		tePrintKeyPressed(key_shifts & KB_ALT_FLAG, "key_shifts: ALT is pressed", 80, 40+text_height(font)*2);

		/* Print results for their equivalent key[] values */
		tePrintKeyPressed(key[KEY_LSHIFT], "key[]: LEFT-SHIFT is pressed", 20, 100);
		tePrintKeyPressed(key[KEY_RSHIFT], "key[]: RIGHT-SHIFT is pressed", 288, 100);
		tePrintKeyPressed(key[KEY_LCONTROL], "key[]: LEFT-CONTROL is pressed", 20, 100+text_height(font));
		tePrintKeyPressed(key[KEY_RCONTROL], "key[]: RIGHT-CONTROL is pressed", 288, 100+text_height(font));
		tePrintKeyPressed(key[KEY_ALT], "key[]: ALT is pressed", 20, 100+text_height(font)*2);
		tePrintKeyPressed(key[KEY_ALTGR], "key[]: ALT-GR is pressed", 288, 100+text_height(font)*2);

		// TODO: other >= KEY_MODIFIERS keys?


	}


	TRACE("\nEnding test...\n");
}



TeBool
teSetup()
{
	if(allegro_init())
	{
		TRACE("Error Initialising Allegro.\n");
		return FALSE;
	}


	if(install_keyboard())
	{
		TRACE("Error Initialising Keyboard.\n");
		return FALSE;
	}


	if(!teSetupGfx())
	{
		TRACE("Error setting up the graphics.\n");
		return FALSE;
	}


	return TRUE;
}



void
teCleanUp()
{
	teCleanupGraphics();

	remove_keyboard();

	allegro_exit();
}



int main(void)
{
	if(!teSetup())
	{
		teCleanUp();
		return -1;
	}


	teDoTest();


	teCleanUp();

	return 0;
}
END_OF_MAIN();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/