1

I'm trying to make a search feature where the user can hold in the control key and type some text to search. I'm using the OnKeyDown/OnKeyUp to trap the control key.

Is there a easy way to check if the key parameter given to the onKeyUp/Down event is a litteral? Or is it possible to convert the char given to OnKeyPressed while holding down the control key to the char it would have been if the control key where not pressed?

Edit:
I need a solution that can handle letters beyond the simple a..z range, like æ, ø å.

It looks like Delphi 2009 got a couple of useful methods in the TCharachter-class; e.g. function IsLetterOrDigit(C: Char): Boolean;

I'm stuck with delphi 2007, though...

Vegar
  • 12,828
  • 16
  • 85
  • 151

3 Answers3

2

The OnKeyDown & OnKeyPress Events have a number of Limitations.

Rather use a TApplicationEvents Component.

In it's OnShortCut Event hander use the following code

procedure TFormA.ApplicationEvents1ShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
  if (Msg.CharCode = Ord('B')) and (HiWord(Msg.KeyData) and KF_ALTDOWN <> 0)  then
  begin
    Handled := True;
   // Do what needs to be done;
  end;

end;

This will trap ALT-B

have a look at Delphi - Using the TApplicationEvents OnShortCut event to detect Alt+C key presses for more info

Community
  • 1
  • 1
Charles Faiga
  • 11,665
  • 25
  • 102
  • 139
1

You can convert the Key(Char) parameter from OnKeyPress event to it's ordinal value using Ord(Key) however, in the OnKeyDown event you can do the following

if CharInSet(Char(Key), ['A'..'Z']) then
  do something

you might also want to play with ShiftState to check if ALT, CTRL and/or SHIFT key is down...

0

Here you have all the info:

Alexander
  • 23,432
  • 11
  • 63
  • 73
RBA
  • 12,337
  • 16
  • 79
  • 126