線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1329
推到 Plurk!
推到 Facebook!

Capturing keyboard messages at application level i

 
conundrum
尊榮會員


發表:893
回覆:1272
積分:643
註冊:2004-01-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-09-23 22:23:15 IP:61.64.xxx.xxx 未訂閱
 http://www.howtodothings.com/showarticle.asp?article=312    Capturing keyboard messages at application level in Delphi    We capture the keyboard messages with the OnMessage event of the Application object. You can find similar articles, but the code presented here is more complete and takes into account certain special cases.    For the ENTER key (VK_RETURN) we want to move to the next control in the case of edit boxes and other controls, so we ask if the active control descends from TCustomEdit, which includes TEdit, TDBEdit, TMaskEdit, TDBMaskEdit, TMemo, TDBMemo and other components provided by third parties. Since we want to exclude TMemo, TDBMemo and, in general, all descendants of TCustomMemo, we make a special proviso in this case (leaving the message unchanged with no action), leaving us with the single-line edit controls, to which we add listboxes, comboxes, etc. For these elements we replace the ENTER key (VK_RETURN) by a TAB key (VK_TAB), both for the WM_KEYDOWN and WM_KEYUP events.    However in the case of a combobox (any TCustomCombobox descendant), when the list is dropped down we wish to maintain the traditional behaviour of the ENTER key (i.e. closing the list).    It would be nice to have a keyboard shortcut for the default button of a form (the button with its Default property set to True), for example CTRL ENTER. This feature is included in the code. The way it is accomplished is a little bit complex to explain... Perhaps it would have been easier to iterate thru the components on a form to find a focuseable button with Default = True, and then call its Click method, but we used a code similar to the one used in VCL forms, which takes into account the fact that the ENTER key might be wanted to get trapped by many controls, not only a button.    We also want the DOWN arrow key (VK_DOWN) to be mapped as a TAB key (VK_TAB). For this case we used a simpler code. Of course, we also want the UP arrow key (VK_UP) to be mapped to a SHIFT TAB key combination. Well, it isn't possible to map a key with a modifier. We can descard the key and simulate the events of pressing VK_SHIFT and then VK_TAB, or we can change the state of the SHIFT key in the keyboard state array (like we did with the CTRL key in the CTRL ENTER combination), but we took a different approach (simply focusing the previous control of the active control in the tab order).    Finally, for Spanish applications, it is usually desirable to replace the decimal point of the numeric keypad with a coma (decimal separator in Spanish).    Well, enough talking, and here's the code:
type
  TForm1 = class(TForm)
    ...
  private
    ...
    procedure ApplicationMessage(var Msg: TMsg; var Handled: Boolean);
    ...
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := ApplicationMessage;
end;    procedure TForm1.ApplicationMessage(var Msg: TMsg;
  var Handled: Boolean);
var
  ActiveControl : TWinControl;
  Form : TCustomForm;
  ShiftState : TShiftState;
  KeyState : TKeyboardState;
begin
  case Msg.Message of
  WM_KEYDOWN, WM_KEYUP:
    case Msg.wParam of
    VK_RETURN:
      // Replaces ENTER with TAB, and CTRL ENTER with ENTER...
      begin
        GetKeyboardState(KeyState);
        ShiftState := KeyboardStateToShiftState(KeyState);
        if (ShiftState = []) or (ShiftState = [ssCtrl]) then begin
          ActiveControl := Screen.ActiveControl;
          if (ActiveControl is TCustomComboBox) and
             (TCustomComboBox(ActiveControl).DroppedDown) then begin
            if ShiftState = [ssCtrl] then begin
              KeyState[VK_LCONTROL] := KeyState[VK_LCONTROL] and $7F;
              KeyState[VK_RCONTROL] := KeyState[VK_RCONTROL] and $7F;
              KeyState[VK_CONTROL]  := KeyState[VK_CONTROL]  and $7F;
              SetKeyboardState(KeyState);
            end;
          end else if (ActiveControl is TCustomEdit)
              and not (ActiveControl is TCustomMemo)
              or (ActiveControl is TCustomCheckbox)
              or (ActiveControl is TRadioButton)
              or (ActiveControl is TCustomListBox)
              or (ActiveControl is TCustomComboBox)
              // You can add more controls to the list with "or"
              then
            if ShiftState = [] then begin
              Msg.wParam := VK_TAB
            end else begin // ShiftState = [ssCtrl]
              Msg.wParam := 0; // Discard the key
              if Msg.Message = WM_KEYDOWN then begin
                Form := GetParentForm(ActiveControl);
                if (Form <> nil) and
                   (ActiveControl.Perform(CM_WANTSPECIALKEY,
                    VK_RETURN, 0) = 0) and
                   (ActiveControl.Perform(WM_GETDLGCODE, 0, 0)
                    and DLGC_WANTALLKEYS = 0) then
                begin
                  KeyState[VK_LCONTROL] := KeyState[VK_LCONTROL] and $7F;
                  KeyState[VK_RCONTROL] := KeyState[VK_RCONTROL] and $7F;
                  KeyState[VK_CONTROL]  := KeyState[VK_CONTROL]  and $7F;
                  SetKeyboardState(KeyState);
                  Form.Perform(CM_DIALOGKEY, VK_RETURN, Msg.lParam);
                end;
              end;
            end;
        end;
      end;
    VK_DOWN:
      begin
        GetKeyboardState(KeyState);
        if KeyboardStateToShiftState(KeyState) = [] then begin
          ActiveControl := Screen.ActiveControl;
          if (ActiveControl is TCustomEdit)
              and not (ActiveControl is TCustomMemo)
              // You can add more controls to the list with "or"
              then
            Msg.wParam := VK_TAB;
        end;
      end;
    VK_UP:
      begin
        GetKeyboardState(KeyState);
        if KeyboardStateToShiftState(KeyState) = [] then begin
          ActiveControl := Screen.ActiveControl;
          if (ActiveControl is TCustomEdit)
              and not (ActiveControl is TCustomMemo)
              // You can add more controls to the list with "or"
              then
          begin
            Msg.wParam := 0; // Discard the key
            if Msg.Message = WM_KEYDOWN then begin
              Form := GetParentForm(ActiveControl);
              if Form <> nil then // Move to previous control
                Form.Perform(WM_NEXTDLGCTL, 1, 0);
            end;
          end;
        end;
      end;
    // Replace the decimal point of the numeric key pad (VK_DECIMAL)
    // with a comma (key code = 188). For Spanish applications.
    VK_DECIMAL:
      begin
        GetKeyboardState(KeyState);
        if KeyboardStateToShiftState(KeyState) = [] then begin
          Msg.wParam := 188;
        end;
      end;
    end;
  end;
end;    
系統時間:2024-05-07 21:42:50
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!