中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi 在应用层截获键盘消息

来源:国外 关于:Ernesto De Spirito 发布时间:2007-07-03   [收藏] [推荐]
Problem/Question/Abstract:
We can migrate old DOS applications to the Windows environment, but frequently we can't migrate the users :)
Answer:
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 SHIFT and then 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;

[浏览: 次]   
上一篇:delphi 固定窗体位置   下一篇:delphi 得到键盘输入的语言
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 消息是由谁来发出又由谁来完成
·delphi 程序缩小为任务右下角的小图标
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932