delphi.gif (306 バイト) HotKeyの使い方


ホットキーコントロールを使うとユーザにホットキー(ショートカットキー)をカスタマイズさせることができます。

delphi1.gif (322 バイト)

このサンプルはメニュー「Test/Increment」と「Test/Decrement」のショートカットをHotKeyコントロールを使って変更するものです。

 

unit frmHotKey;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    HotKeyDec: THotKey;
    Label1: TLabel;
    TrackBar1: TTrackBar;
    MainMenu1: TMainMenu;
    Test1: TMenuItem;
    Increment1: TMenuItem;
    Decrement1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    HotKeyInc: THotKey;
    Label2: TLabel;
    cmdSetShortCut: TButton;
    procedure Exit1Click(Sender: TObject);
    procedure cmdSetShortCutClick(Sender: TObject);
    procedure Increment1Click(Sender: TObject);
    procedure Decrement1Click(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Exit1Click(Sender: TObject);
begin
  Close
end;

procedure TForm1.cmdSetShortCutClick(Sender: TObject);
begin
    MainMenu1.Items[0].Items[0].ShortCut := HotKeyInc.HotKey; // ここで変更している
    MainMenu1.Items[0].Items[1].ShortCut := HotKeyDec.HotKey;
end;

procedure TForm1.Increment1Click(Sender: TObject);
begin
   if TrackBar1.Position < TrackBar1.Max then
	TrackBar1.Position := TrackBar1.Position + 1;
end;

procedure TForm1.Decrement1Click(Sender: TObject);
begin
    if TrackBar1.Position > TrackBar1.Min then
	TrackBar1.Position := TrackBar1.Position - 1;
end;

end.