Prog1020
Prog1020

Reputation: 4781

Delphi: Win key allowed in shortcuts?

Can I set somehow for TMainManu items shortcuts with WIN key? E.g. set "Win+Alt+S". Does TShortcut type support WIN key. I use Delphi 7.

Upvotes: 0

Views: 825

Answers (3)

Ken White
Ken White

Reputation: 125767

This cannot be done using TShortCut, with either the menu designer or in code. Dropping a TMainMenu on a form, adding a TMenuItem, and trying to assign the Win raises an "invalid property value" exception, and it doesn't work when trying to do so with KeyToShortCut in code either.

The only modifiers you should use for shortcuts are Shift, Ctrl, and Alt, according to Microsoft. The Win key belongs to Windows. That's why it has that name :-)

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613602

You cannot respond to such a shortcut from the ShortCut property of a VCL control, at least so far as I can tell. Delphi's shortcut mechanism won't treat the Windows key as a modifier. Therefore you would need to include it as a non-modifier key. But a Delphi shortcut can only refer to a single non-modifier key, and you'd need two non-modifiers for your key press.

However, you can add your own bespoke handling and respond to such a key press. For example in the OnShortCut event of your form. This is very crude, but illustrates that it is possible.

procedure TMyForm.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
  if (GetKeyState(VK_LWIN)<0) and (GetKeyState(VK_MENU)<0) then begin
    if Msg.CharCode = ord('S') then begin
      // do something
      Handled := True;
    end;
  end;
end;

Now, I'm not going to attempt to tidy this up, or do it properly, since the guidelines are very clear that you should not respond to shortcuts involving the Windows key. MSDN says:

Keyboard shortcuts that involve the WINDOWS key are reserved for use by the operating system.

I just wanted to prove that it is perfectly possible to handle such key presses in your application.

Upvotes: 6

Chris Thornton
Chris Thornton

Reputation: 15817

You CAN, however, use the windows key as part of global hotkeys.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx So you COULD make it seem like it was acting as a shortcut, but in fact, would be system-wide. So you SHOULD not do it.

Upvotes: 1

Related Questions