Recent

Author Topic: ComboBox KeyDown event  (Read 3557 times)

andy.int

  • Newbie
  • Posts: 3
ComboBox KeyDown event
« on: August 16, 2018, 03:55:45 am »
Hi,
I am using the KeyDown event of ComboBox.
The keys ESC, TAB, Enter have the same key value (Key= 0), is this correct?
How do I get the correct key value?
Thank you.

Environment:
Windows 10 Home
lazarus-1.8.4-fpc-3.0.4-win64.exe

Beginner, andy.int

Code: Pascal  [Select][+][-]
  1. procedure TForm_main.ComboBox_market_KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4. Caption := Caption + 'KeyDown(' + IntToStr(Key ) + ') ';  // For test
  5.  
  6.   case Key of
  7.   VK_F4: begin  // Disable F4
  8.          Key := 0;
  9.          end;
  10.   VK_ESCAPE: begin                            
  11.              //Key := 0;
  12.              // Do something            
  13.              end;
  14.   VK_TAB: begin      
  15.           //Key := 0;
  16.           // Do something          
  17.           end;
  18.   VK_RETURN: begin        
  19.              //Key := 0;
  20.              // Do something
  21.              end
  22.   end  // case Key of
  23. end;
  24.  

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: ComboBox KeyDown event
« Reply #1 on: August 16, 2018, 04:14:12 am »
Hello andy.int,
Welcome to the forum.

I tested on Linux, this code below works for me:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   Caption := 'KeyDown(' + Key.ToString + ') ';
  5.   case Key of
  6.     VK_F4: begin  // Disable F4
  7.            Key := 0;
  8.            end;
  9.     VK_ESCAPE: begin
  10.                //Key := 0;
  11.                // Do something
  12.                end;
  13.     VK_TAB: begin
  14.             //Key := 0;
  15.             // Do something
  16.             end;
  17.     VK_RETURN: begin
  18.                //Key := 0;
  19.                // Do something
  20.                end
  21.   end
  22. end;

andy.int

  • Newbie
  • Posts: 3
Re: ComboBox KeyDown event
« Reply #2 on: August 16, 2018, 06:56:26 pm »
Hi Handoko,

Thank you for your answer.

I got a result after the test.
When the ComboBox is pulled down, the keys ESC, TAB, Enter have the same key value (Key = 0).

When the ComboBox is closed, I got
ESC Key= 27
TAB Key= 9
Enter Key= 13
These are the correct key values.

But I hope I can get it when the ComboBox is pulled down. Just like the Key F4.

Thanks.

PS. I like "Key.ToString" ^_^

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: ComboBox KeyDown event
« Reply #3 on: August 16, 2018, 07:24:47 pm »
But I hope I can get it when the ComboBox is pulled down. Just like the Key F4.

I just tested the code on a Win7 computer, it worked even when the ComboBox was pulled down. But no matter how I tried, it won't call the OnKeyDown on Linux when the ComboBox is pulled down. The behavior is OS-depended I think. Have you tried the code on Win7?

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: ComboBox KeyDown event
« Reply #4 on: August 17, 2018, 12:40:25 am »
The combo box in windows are two different controls, an TEDIT and TLISTBOX..

 The two controls don't seem to act together as one like they should, the keyboard input is getting
captured by the EDIT box until you select something in the list box and when you hover over the list box you don't receive mouse messages...

  To get better control over the box I've found using the OwnerDraw seems to work

 Try setting the Style to OwnerDraw and handle the OnDrawItem, I Think you should be able to receive all
after that...

The only true wisdom is knowing you know nothing

andy.int

  • Newbie
  • Posts: 3
Re: ComboBox KeyDown event
« Reply #5 on: August 17, 2018, 03:18:31 am »
Hi jamie,

I tried it, just like you said.
Thank you for your advice. Now I am trying to use the OnDrawItem event...

To Handoko,
I have tried different OS,
Win8: Same as Win10.
Win7: none
WinXP: (Rebuild code) Same as Win10.

Thank you so much.

 

TinyPortal © 2005-2018