StringGrid OnKeyPress not catching every key event lazarus / 31-LCL ---------------------------------------------------------------------- ralfm · 2013-12-05 18:19 Dear all, I encountered a very strange problem with the OnKeyPress event in string grids. Maybe someone could verify this and/or provide a solution. I've created a simple TForm and placed a single TStringGrid on it. The Grid consists of two columns, each with ButtonStyle 'cbsAuto'. To limit user input to a certain set of characters, I used the OnKeyPress event of the grid, which points to the following code: Code: [Select]procedure TForm1.sgTestKeyPress(Sender: TObject; var Key: char); begin   case sgTest.Col of     1: if not (Key in ['0'..'9','-','*',#8,#9,#13]) then Key := #0;     2: if not (Key in ['0'..'9','.',#8,#9,#13]) then Key := #0;   end; end; Basically this limits the user input to just numeric characters when editing a cell. This works as expected if I type "normally". But if I very rapidly hit random keys on my keyboard, the cell also occassionally accepts characters (eg. alphabetical or special chars) which should be excluded by the above procedure. I'm baffled! Did I miss something? Environment: Ubuntu 12.04.3 LTS Lazarus 1.0.14/2013-11-16 FPC version 2.6.2 SVN version 43447M x86_64-linux-gtk2 Any help is very much appreciated! Ralf ---------------------------------------------------------------------- engkin · 2013-12-05 19:52 Can you repeat your tests with this code: Code: [Select]procedure TForm1.sgTestKeyPress(Sender: TObject; var Key: char); begin   if not (Key in ['0'..'9','-','*',#8,#9,#13]) then Key := #0; end; I really have no idea, but your code would let a key pass if sgTest.Col is not 1 or 2. The previous code would eliminate that possibility. You might want to try OnKeyDown instead. ---------------------------------------------------------------------- ralfm · 2013-12-05 20:28 Quote from: engkin on December 05, 2013, 08:52:13 pmCan you repeat your tests with this code: Code: [Select]procedure TForm1.sgTestKeyPress(Sender: TObject; var Key: char); begin   if not (Key in ['0'..'9','-','*',#8,#9,#13]) then Key := #0; end; I really have no idea, but your code would let a key pass if sgTest.Col is not 1 or 2. The previous code would eliminate that possibility. Thanks for the reply. I tried OnKeyPress with the code you suggested as well as OnKeyDown with this code: Code: [Select]procedure TForm1.sgTestKeyDown(Sender: TObject; var Key: Word;   Shift: TShiftState); begin   if not (Key in [VK_RETURN, VK_TAB, VK_0, VK_1, VK_2, VK_3, VK_4, VK_5,             VK_6, VK_7, VK_8, VK_9]) then Key := 0; end; I even restricted it further to Code: [Select]if (Key <> VK_TAB) then Key:=0; but the behaviour didn't change. I still get the "unwanted" chars if I rapidly hit random keys. EDIT: Additionally I tried this on another machine running Ubuntu 13.04 (same compiler specs as stated) and get the same results! Can someone confirm this behaviour? Ralf ---------------------------------------------------------------------- engkin · 2013-12-05 21:02 Using Windows, I did not get any "unwanted" chars, but it did not prevent me from hitting Shift+Insert and having whatever value stored in the clipboard copied into the active cell. The same applies to Menu key and letter P (for paste), and of course right clicking my mouse and choosing Paste. I don't know, maybe I was not hitting my keys vigorously. ---------------------------------------------------------------------- Avishai · 2013-12-05 23:12 I don't think this will help your problem, but your code doesn't allow for enough Keys.  You need to allow for navigation keys and... I use this code for TEdit for a long time: Code: [Select]procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); const   GoodKeys = [     VK_0..VK_9,     VK_NUMPAD0..VK_NUMPAD9,     VK_Left,VK_Right,     VK_BACK,VK_DELETE,     VK_END,VK_HOME,     VK_RETURN,VK_TAB,     VK_SUBTRACT,     VK_DECIMAL,     VK_MULTIPLY   ]; begin   if not(Key in GoodKeys) then Key:= 0; end; ---------------------------------------------------------------------- jwdietrich · 2013-12-05 23:53 I cannot reproduce your problem on Mac OS X (PPC processor, OS X 10.5.8, Lazarus 1.0.14, FPC 2.6.2, Carbon widget set). Here, the grid only accepts keys that are defined via the if-clause. The problem may be widgetset-dependent. ---------------------------------------------------------------------- howardpc · 2013-12-06 09:02 It might more likely be a hardware-related effect. Event handlers take a finite time to execute and return (and grids are large, complex classes with usually many calls happening 'simultaneously'). Key events are handled by the underlying OS as messages, and it is conceivable that by typing furiously you send succesive key messages to your program's window procedure before some keyboard event handler in your program has returned, and so the filtering of an occasional message gets skipped. Well, perhaps a systems programmer will tell us this is impossible, but it seems feasible in theory at least, to me. ---------------------------------------------------------------------- taazz · 2013-12-06 09:36 Quote from: howardpc on December 06, 2013, 10:02:27 amKey events are handled by the underlying OS as messages, and it is conceivable that by typing furiously you send succesive key messages to your program's window procedure before some keyboard event handler in your program has returned, and so the filtering of an occasional message gets skipped. True in most part, if we talk about windows then the messages are put in a message queue and then the main application loop dispatch them one by one it can not dispatch the next unless the previous one returns from its loop so no its not possible to reenter the same handler before it exists or to loose messages for that matter. ---------------------------------------------------------------------- ralfm · 2013-12-06 16:38 Dear all, I made some additional tests with the following results: I can confirm engkin's statement: Quote from: engkin on December 05, 2013, 10:02:55 pmUsing Windows, I did not get any "unwanted" chars... I compiled the program on a Windows machine (Win7 Professional x64, Laz 1.0.14). The program does not have the effects I experience on Linux GTK. I also think, it is not hardware related as howardpc suspected: Quote from: howardpc on December 06, 2013, 10:02:27 amIt might more likely be a hardware-related effect. I draw this conclusion from the facts, that I have the same effect on two different machines running Linux and one of these machines is the same physical hardware I made the test on Windows (dual boot ftw). In conjunction with Quote from: jwdietrich on December 06, 2013, 12:53:04 amI cannot reproduce your problem on Mac OS X (PPC processor, OS X 10.5.8, Lazarus 1.0.14, FPC 2.6.2, Carbon widget set). Here, the grid only accepts keys that are defined via the if-clause. The problem may be widgetset-dependent. I second the assumption of a widgetset dependency. Can someone with a similar setup as mine (see first post) please confirm this? Ralf ---------------------------------------------------------------------- Martin_fr · 2013-12-15 13:26 GTK2 only, or QT too? This reminds me of http://bugs.freepascal.org/view.php?id=14716 See if the patch on that issue can be applied (to gtk2, the patch is for gtk) The code is similar though.... In lcl\interfaces\gtk2\gtk2proc.pp line 870 and 880   Event^.keyval; must also be checked. And for that a field must be added to the structure  (line 855)    TLCLHandledKeyEvent = class ... --- Note, that this may cause other bugs. It simple is to check, if your system sends multiple independent keystrokes with the same timestamp. ---------------------------------------------------------------------- ralfm · 2014-01-07 21:46 Quote from: Martin_fr on December 15, 2013, 02:26:27 pmGTK2 only, or QT too? As far as I can say: GTK2 related. Quote from: Martin_fr on December 15, 2013, 02:26:27 pmThis reminds me of http://bugs.freepascal.org/view.php?id=14716 See if the patch on that issue can be applied (to gtk2, the patch is for gtk) As suggested, I applied the patch from issue #14716 and it indeed seems to work. With the patch applied the behaviour appears to be as expected. I will leave it patched to observe if any side effects arise. So far I haven't noticed any. Thanks a lot! Ralf