开发者

Delphi: Convert keys to letters, ignoring shiftsate

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 i开发者_Python百科t 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...


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


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...


Here you have all the info:

  • Virtual Key Codes
  • Understanding and Processing Keyboard events in Delphi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜