2011. május 20., péntek

Capturing keyboard messages at application level


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;

Copyright (c) 2001 Ernesto De Spirito
Visit: http://www.latiumsoftware.com/delphi-newsletter.php

Nincsenek megjegyzések:

Megjegyzés küldése