PDA

View Full Version : Keyboard unit: kbReleased problem.



Christian Knudsen
18-04-2008, 02:04 PM
I've asked this question at the FreePascal site, but haven't received a reply for over a week, so I'm trying here as well:

I want to use the Keyboard unit that comes with FreePascal to keep track of which keys are currently pressed. In my code I have an array of all keys. I can set a key as pressed by using PollEvent to get keypress events, but I'm unable to get an event when the key is released. It is my understanding that the kbReleased flag should indicate a key release event, but I'm never getting this flag. Here's an excerpt from my test code:


Repeat
K:=GetKeyEvent;
K:=TranslateKeyEvent(K);
Write('Got key event with ');
Case GetKeyEventFlags(K) of
kbReleased : Writeln('Released key event');
end;
Writeln('Got key : ',KeyEventToString(K));
Until (GetKeyEventChar(K)='q');


It reads keypresses as it should, but I never get the 'Released key event' message.

grudzio
18-04-2008, 05:24 PM
I think you should check for release event like this:

flags := GetKeyEventFlags(K);
if (flags and kbReleased) <> 0 then
writeln('Released key event'):


I suspect that since flags is a byte it can hold many flags joined together by binary or.

Christian Knudsen
18-04-2008, 07:03 PM
That didn't work. I've even tried using the example that comes with FreePascal, but that didn't work either:

program example1;

{ This program demonstrates the GetKeyEvent function }

uses keyboard;

Var
K : TKeyEvent;

begin
InitKeyBoard;
Writeln('Press keys, press "q" to end.');
Repeat
K:=GetKeyEvent;
K:=TranslateKeyEvent(K);
Write('Got key event with ');
Case GetKeyEventFlags(K) of
kbASCII : Writeln('ASCII key');
kbUniCode : Writeln('Unicode key');
kbFnKey : Writeln('Function key');
kbPhys : Writeln('Physical key');
kbReleased : Writeln('Released key event');
end;
K:=TranslateKeyEvent(K);
Writeln('Got key : ',KeyEventToString(K));
Until (GetKeyEventChar(K)='q');
DoneKeyBoard;
end.


Can anybody get the above code to report 'Released key event'?

cronodragon
18-04-2008, 08:25 PM
Months ago I did a lot of testing to retrieve the Release flag, and seems it's not working at all. After asking for it the FPC mailing list, I finally gave up, and moved to platform specific functions. :?

Christian Knudsen
18-04-2008, 08:56 PM
Well, that's pretty disappointing. They should put that information somewhere on the FreePascal site to keep people from wasting a lot of time getting it to work...