How to add Shift + Ctrl + HX key binding to Delphi environment using ToolsApi?

Adding a new ShortCut to the Delphi IDE is not too difficult, because the open tools API provides a service for this. I am trying to do something more complex: add Wordstar as an optional ShortCut:

I want something to happen when the user clicks

Shift + Ctrl + H followed by one X key

where X should work regardless of the state of the Shift key.

This is my code:

procedure TGxKeyboardBinding.BindKeyboard(const BindingServices: IOTAKeyBindingServices); const DefaultKeyBindingsFlag = kfImplicitShift + kfImplicitModifier + kfImplicitKeypad; var GExpertsShortcut: Byte; ShiftState: TShiftState; FirstShortCut: TShortCut; SecondShortCut: TShortCut; begin GExpertsShortcut := Ord('H'); ShiftState := [ssShift, ssCtrl]; FirstShortCut := ShortCut(GExpertsShortcut, ShiftState); SecondShortCut := ShortCut(Ord('X'), []); BindingServices.AddKeyBinding([FirstShortCut, SecondShortCut], TwoKeyBindingHandler, nil, DefaultKeyBindingsFlag, '', ''); end; 

So, if I set ShiftState: = [ssCtrl] by clicking

Ctrl + HX

calls my TwoKeyBindingHandler method.

But with ShiftState: = [ssShift, ssCtrl] click

Shift + Ctrl + HX

doing nothing.

Oddly enough, when specifying ShiftState: = [ssShift, ssCtrl] (which should only affect the first key) by pressing

Shift + Ctrl + H Shift + X

calls my TwoKeyBindingHandler method, although the second ShortCut is added without a modifier key.

Any idea? Perhaps this is a known limitation / bug of the Delphi IDE / Open Tools APIs? Is a workaround known?

I tried this in Delphi 2007 and Delphi 10 Seattle, no difference.

+5
source share
2 answers

You can do this using the GetKeyState function.

The program has two operations. Think about it by opening the dropdown menu item. When ctr-shift-h is pressed, your program should indicate that the "Menu" is now open, and that subsequent keystrokes will either activate the option or close the "menu" if an invalid key is pressed.

 function IsKeyDown(const VK: integer): boolean; begin IsKeyDown := GetKeyState(VK) and $8000 <> 0; end; procedure Form1.OnkeyDown(...) begin if Not H_MenuOpen then if IsKeyDown(vk_Control) and IskeyDown(vk_Shift) and IsKeyDown(vk_H) then begin //Some Boolean in the form H_MenuOpen:=True; //Will probably need to invalidate some parameters here so that //no control tries to process the key exit; end; if H_MenuOpen then begin if key=vk_X then begin //x has been pressed *Your code here* //possibly invalidate some of the params again exit; end; //Nothing valid H_MenuOpen:=False; end; 

end;

+3
source

OK, since apparently no one has found the answer, here is what I ended up doing:

I already planned to show a help window that lists all the possible characters for the second key (in fact, this code already worked fine using the approach proposed by Helen Fergrivit in her answer to this question). Instead, I now register only a single key combination:

 BindingServices.AddKeyBinding([FirstShortCut], TwoKeyBindingHandler, nil, DefaultKeyBindingsFlag, '', ''); 

And in the TwoKeyBindingHandler method, I show a popup menu containing these characters as shortcuts. Then the IDE / VCL / Windows handles the rest for me.

It looks like this: animated GIF with the result

This is not the answer to the real question, but it solves my problem. Sorry if you are expecting anything more here.

+1
source

Source: https://habr.com/ru/post/1241940/


All Articles