2
votes

im working on delphi 7, and i have an applicaton where im using a action manager for creating actions and then assigning them shortcuts the shortcuts in action manager are already defined in the shortcut property..but i want to have a shortcut like

 1. ADD ,that is the + on the numpad
 2. Subtract key ,that is the - on the numpad
 3. divide key  , that is the / on the numpad.
 4. enter key

i tried assigning my own shortcut as subtract and ADD but it gives me this error message enter image description here

i also tried - but nothing is happening

my code to track the action

  var
      Action : TBasicAction;
      begin
        Action := Sender as TBasicAction;
       if (Action is TAction) and not TAction(Action).Enabled then   exit;
      case Action.tag of
        1                  :         ShowMessage('ADD + pressed');
        2                  :         ShowMessage ('divide / pressed');
        3                  :         ShowMessage ('subtract - pressed');
        4                  :         ShowMessage ('enter pressed');
      end;

   end;

i can use the normal

   **GetKeyState(VK_ADD) AND 128)=128;** OR **GetKeyState(VK_return) AND 128)=128;** 

to find it the keys are pressed in keypress or keydown event but i want to use the action manager shortcuts

3
I would think that the fact that 'subtract' and 'add' are not in the dropdownlist of the ShortCut property would be enough hint that you cannot use them as shortcuts? At least not through the action/actionmanager framework... - Marjan Venema
I think you can't use short cuts for this and will have to use key events. You can always do Action.Execute. Be aware that most computers sold today don't have the keys that you are describing. - David Heffernan
@David, define "most". I have two laptops that both have full numeric keypads, a third that has no keypad but when FN is used to enable the embedded one has the above keys, three desktops (2 home, 1 office) that all have the full numeric keypad including those keys. One laptop is recent (6 months), and the work desktop was brand new two months ago. - Ken White
@ken I mean most sold as opposed to most bought by you! ;-) - David Heffernan

3 Answers

8
votes

You can assign them at runtime:

Action1.ShortCut := menus.ShortCut(VK_ADD, []);
Action2.ShortCut := menus.ShortCut(VK_SUBTRACT, []);
7
votes

These keys have the following special names which can be assigned directly through the ShortCut property in the Object Inspector.

  • NUM ADD
  • NUM SUB
  • NUMMULT
  • /

I'm not sure about the numpad Enter key. I don't know if it has a special name or indeed if it can be distinguished from the normal Enter key using a shortcut.

Update

It turns out that the names for these keys vary from machine to machine. François points out that the names come from the GetKeyNameText Windows API function, for which the documentation points out:

The format of the key-name string depends on the current keyboard layout. The keyboard driver maintains a list of names in the form of character strings for keys with names longer than a single character. The key name is translated according to the layout of the currently installed keyboard, thus the function may give different results for different input locales.

For example, Sertac found that on his system, the names are NUM +, NUM - etc.

I generated the values by calling ShortcutToText and you could do the same to generate names appropriate for your system.

1
votes

You can't just make up names for keys and expect the Object Inspector to know what you mean. The ShortCut property isn't a string; it's a TShortCut, and can only be assigned a value that can be converted to a TShortCut.

There are no keys on the keyboard named ADD, subtract, or divide, and so they can't be assigned as such to a ShortCut; they don't exist, so they can't be converted to a TShortCut.

You can set the shortcut directly to +, -, /, and Enter (the values +, -, /, and Enter), and they should work fine. (Although assignment of common single keystrokes is usually not a good idea - if the form only has a single thing it can do in response to the + key, it might be fine; if there's an edit control or something that could also receive a + key sensibly then it's a terrible idea because the shortcut's action might fire when the user didn't intend it to do so).

As David points out, this won't pick up the number keypad part of the requirement. For that, you'll have to assign the ShortCut in code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  AddAction.ShortCut := Shortcut(VK_ADD, []);
  SubtractAction.ShortCut := ShortCut(VK_SUBTRACT, []);
  DivideAction.ShortCut := ShortCut(VK_DIVIDE, []);
end;

I can't find any way to differentiate between the NUMPAD Enter and the regular enter using virtual key definitions, though. You may need to do that with a modifier or in the keypress events.

As far as figuring out which one was used, you should be naming them appropriately (as in AddAction, SubtractAction, DivideAction, and EnterAction), and then you can actually reference them by name in the OnUpdate or OnExecute events:

if (Sender = TAddAction) then // Add keypress fired
  DoAddAction
else if (Sender = TSubtractAction) then
  DoSubtractAction
// etc