tags:

views:

701

answers:

5

Hi all,

Is there anyway to get the THotkey component in delphi to support the windows key?

Or does anyone know of a component that can do this?

Thanks heaps!

+3  A: 

I don't know if you can do it with the THotkey component.

But you can capture the left and right Windows Key in any KeyDown event using:

if key = vk_LWin then showmessage('left');
if key = vk_RWin then showmessage('right');

lkessler
Lol, I was just typing that answer. You must have a faster keyboard.
Gamecat
+7  A: 

IMHO it is a good thing THotKey does not support this.

Don't use the windows key for keyboard shortcuts in your program, the "Windows Vista User Experience Guidelines" says the following under Guidelines - Interaction - Keyboard:

Don't use the Windows logo modifier key for program shortcut keys. Windows logo key is reserved for Windows use. Even if a Windows logo key combination isn't being used by Windows now, it may be in the future.

Even if the shortcut isn't used by Windows, using such a keyboard shortcut would be confusing to users, as it would perform a function in your program, while other such shortcuts like Win+E or Win+R activate a system-wide function, deactivating your program in the process.

Edit:

THotKey is a light wrapper around a system control, supporting only the things that this system control supports. There is no documented way to set anything but the Alt, Ctrl and Shift modifiers for the shortcut.

You might be able to create your own control to display shortcuts using the Windows key, and set a global keyboard hook (look into the SetWindowsHookEx() API function).

mghie
The shortcuts that will be used are to be user customised, not hard codedThey then get set as a global shortcut so they can activate and de-activated using that shortcut without having the application active. This is working but I wanted to give the user the option to use the windows key as well.
HannesNZ
I added some information regarding your use case. Probably this would be a lot of work, don't know whether it's important enough. There is of course also the problem that creating a global hook may not work for all users (missing permissions).
mghie
+3  A: 

See RegisterHotKey function on MSDN.

liggett78
Good tip, didn't know that function.
mghie
This is already what i'm using to register the hotkey, but to get the shortcut to use from the user i'm using a THotKey component. but that is the one that doesn't support the win key, not the 'RegisterHotKey' function. I might just implement the user input a different way.
HannesNZ
+1  A: 

THotKey doesn't support the Win-Key. I would add a check box next to it maybe for the Win-Key modifier.

Jim McKeeth
Thanks, something like that is what i'll do to get this working.
HannesNZ
you can tweak Thotkey to support it, see my reply below..
Tom
+3  A: 

Sure its possible - you need to make your own copy of { THotKey } and tweak it a little to support also Win key. You need to add your own KeyDown() and Repaint() functions to this class .

Like this:

  TMyCustomHotKey = class(TWinControl)
  public
      WinKey: boolean;
  procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
    constructor Create(AOwner: TComponent); override;
  end;

  TMyHotKey = class(TMyCustomHotKey)

..

    procedure TMyCustomHotKey.KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    var
     a : integer;
     lbl : string;
      tmphot : tshortcut;
    begin

     a:= 0;
      if GetAsyncKeyState(VK_LWIN) <> 0 then  a:= 1;
      if GetAsyncKeyState(VK_RWIN) <> 0 then a:= 1;

     if a=1 then begin
            winkey := true;
      end else
      begin
            winkey := false;
      end;
      rePaint();
}


procedure TMyCustomHotKey.WMPaint(var Message: TWMPaint);
var
  PS: TPaintStruct;
  DC: HDC;
  Canvas: TCanvas;
  i: Integer;
  X, Y: Integer;
  OldColor: TColor;
  Size: TSize;
  Max: Integer;
  s, Palabra, PrevWord: string;
    OldPen, DrawPen: HPEN;
 tmphot : tshortcut;
  Key: Word;
  Shift: TShiftState;

  lbl ,res: string;
 keyboardState: TKeyboardState;
asciiResult: Integer;

begin

  DC := Message.DC;
  if DC = 0 then DC := BeginPaint(Handle, PS);

  Canvas := TCanvas.Create;
  try

     OldColor         := Font.Color;
    Canvas.Handle    := DC;
    Canvas.Font.Name := Font.Name;
    Canvas.Font.Size := Font.Size;
    with Canvas do
    begin

      Brush.Color := Self.Color;
      FillRect(Self.ClientRect);
          Font.Color := OldColor;

       tmphot := gethotkey;
         ShortCutToKey(tmphot, Key, Shift);

          res := GetCharFromVKey(key);


         if (winkey = false) and (key = 0 ) and (tmphot = 0)then
          BEGIN lbl := 'Enter hotkey [CTRL/ALT/WIN] + Key'   ;
          TextOut(1 ,1,lbl)             ;
          END
         else begin

         if winkey then lbl := 'Win +' else lbl := '';
          if ssAlt in Shift then lbl := lbl+ 'Alt + ';
          if ssShift in Shift then lbl := lbl+ 'Shift + ';
          if (not winkey) and (ssCtrl  in Shift) then lbl := lbl+ 'Ctrl + ';
          lbl := lbl+ res;

          end;

          TextOut(1 ,1,lbl);



      end;

  finally
    if Message.DC = 0 then EndPaint(Handle, PS);
  end;
  Canvas.Free;
    SETCARETPOS(1,1);

end;
Tom
This code completely breaks on international Windows versions.
mghie