首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ToolsApi将键绑定Shift+Ctrl+H X添加到Delphi IDE?

如何使用ToolsApi将键绑定Shift+Ctrl+H X添加到Delphi IDE?
EN

Stack Overflow用户
提问于 2016-02-01 00:37:47
回答 2查看 661关注 0票数 5

向Delphi IDE添加新的ShortCut并不太困难,因为Open Tools API为此提供了一个服务。我正在尝试一些明显更复杂的东西:添加一个像additional ShortCut这样的单词:

当用户按下时,我希望发生一些事情

Shift+Ctrl+H后跟单键X

无论Shift键的状态如何,X都应在其中工作。

这是我的代码:

代码语言:javascript
复制
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;

因此,如果我将ShiftState := ssCtrl设置为

Ctrl+H X

调用我的TwoKeyBindingHandler方法。

但是对于ShiftState := ssShift,ssCtrl按下了

Shift+Ctrl+H X

什么也不做。

奇怪的是,在指定ShiftState := ssShift, ssCtrl

Shift+Ctrl+H Shift+X

调用我的TwoKeyBindingHandler方法,即使添加的第二个ShortCut没有修饰符键。

有什么想法吗?这可能是Delphi IDE/Open Tools API的一个已知限制/错误?是否有已知的解决方法?

我在Delphi 2007和Delphi 10西雅图上尝试过,没有区别。

EN

回答 2

Stack Overflow用户

发布于 2016-02-01 05:02:11

您应该能够使用GetKeyState函数来完成此操作。

该程序有两个操作--可以把它看作是打开一个下拉菜单项。当按下ctr-shift-h时,你的程序需要标记“菜单”现在是打开的,如果按下了无效的键,随后的按键将激活一个选项或关闭“菜单”。

代码语言:javascript
复制
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;

结束;

票数 2
EN

Stack Overflow用户

发布于 2016-02-04 03:20:20

好吧,既然显然没有人找到答案,这就是我最终要做的:

相反,我现在只注册了一个快捷键:

代码语言:javascript
复制
BindingServices.AddKeyBinding([FirstShortCut],
  TwoKeyBindingHandler, nil,
  DefaultKeyBindingsFlag, '', '');

在TwoKeyBindingHandler方法中,我显示了一个弹出式菜单,其中包含这些字符作为快捷方式。然后,IDE/VCL/Windows会替我处理剩下的事情。

它看起来是这样的:

这不是对实际问题的回答,但它解决了我的问题。抱歉,如果你来这里期待更多的东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35116298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档