2016-01-31 19 views
5

將新的ShortCut添加到Delphi IDE並不太困難,因爲Open Tools API爲此提供了一項服務。我想顯然更復雜的東西:添加WordStar的像附加快捷方式:如何使用ToolsApi將鍵綁定Shift + Ctrl + H X添加到Delphi IDE中?

我想一些事情發生,當用戶按下

SHIFT + CTRL + H其次是單鍵X

其中X應無論Shift鍵的狀態如何都可以工作。

這是我的代碼:

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 Seattle中試過它,沒有什麼區別。

回答

3

你應該可以使用GetKeyState函數來完成它。

該程序有兩個操作 - 認爲它打開一個下拉菜單項。當ctr-shift-h被按下時,你的程序將需要標記'Menu'現在已經打開,隨後的按鍵將激活一個選項或者如果一個無效鍵被按下時關閉'menu'。

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;

+0

謝謝,這是一般檢查熱鍵的解決方案,但它不適用於我沒有處理OnKeyDown的表單的特定情況。 (除此之外:多次調用IsKeyDown似乎並不是很好的性能,我可能只會調用一次GetKeyState並檢查組合鍵。) – dummzeuch

+0

儘管如此,您仍然可以處理應用程序的wm_keydown消息。 –

+1

是的,我可以。如果我找不到使用Open Tools API的解決方案,我可能會這樣做。不幸的是,我還必須阻止IDE甚至看到可能需要一些額外代碼的密鑰。但是當我到達那裏時我會穿過那座橋。 – dummzeuch

1

OK,因爲顯然沒有人找到了答案,這裏是我落得這樣做:

我已經計劃顯示一個提示窗口,列出所有可能字符的第二個關鍵(實際上該代碼已經工作很好,使用Helen Fairgrieve在她回答這個問題時提出的方法)。相反,我現在只登記一鍵快捷方式:

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

而在TwoKeyBindingHandler方法,我將展示包含這些字符作爲快捷鍵彈出菜單。 IDE/VCL/Windows然後爲我處理其餘部分。

這是什麼樣子: animated GIF with the result

這不是一個問題的實際問題,但它解決了我的問題。對不起,如果你在這裏期待更多的東西。

相關問題