Jump to content
Carlos Tré

Tools API - Changing Key Assignment

Recommended Posts

Dear fellows,

 

I'd like to change the format code action assignment to Ctrl+Alt+Shfit+F, because Ctrl+D is very easy to hit unintentionally. Based on articles I found ou there I was able to simply kill it, but now I need to simply reassign its hot key, and I am at a total loss here.

 

I have a very vage remembrance of coming across sample code for OTAPI that was an actual keyboard mapping, it was quite a while ago , I'm not sure I really did. I took a wild guess from what found and tried

 

 

The relevant section of wat I did so far is 

 

procedure TMelhoriasEditor.BindKeyboard(const BindingServices: IOTAKeyBindingServices);
var
  LBindingRec       :  TKeyBindingRec;
  LFmtKeyBidingProc : TKeyBindingProc;

begin
  (BorlandIDEServices as IOTAKeyboardServices)
    .LookupKeyBinding([Shortcut(Ord('D'), [ssCtrl])],LBindingRec);
  LFmtKeyBidingProc := LBindingRec.KeyProc;
  BindingServices.AddKeyBinding(
    [
      Shortcut(
        Ord('F'),
        [ssCtrl,ssAlt,ssShift]
      )
    ],
    LFmtKeyBidingProc,
    nil
  );
  BindingServices.AddKeyBinding([Shortcut(VK_INSERT, [ssCtrl,ssAlt,ssShift])], AlternarModoInsercao, nil);
  BindingServices.AddKeyBinding([Shortcut(VK_INSERT, [])], MatarTecla, nil);
  BindingServices.AddKeyBinding([Shortcut(VK_RETURN, [ssCtrl])], InserirLinhaAbaixo, nil);
  BindingServices.AddKeyBinding([Shortcut(VK_RETURN, [ssShift])], InserirLinhaAbaixo, nil);
//  BindingServices.AddKeyBinding([Shortcut(Ord('D'), [ssCtrl])], MatarTecla, nil);
end;

Could some kind soul put me in the right track, please? Also, am I dreaming about having seen such code for a keyboard mapping? 

 

Thank you very much.

 

-- Carlos

 

 

Share this post


Link to post

I hope someone can offer a solution, as I find some of the newly added shortcuts in Sydney conflict with some in my plug-ins.

  • Like 1

Share this post


Link to post

Ctrl+D is listed in the GExperts Keyboard Shortcuts expert as assigned to the action actFormatSource, so it's not an editor key binding but simply an action. And that means it's just a matter of finding that action and changing its keyboard shortcut to whatever you like. And the IDE is even helpful with an NTA (native tools api) call for this:

function TGxActionBroker.GetIdeActionList: TCustomActionList;
var
  NTAServices: INTAServices;
begin
  Assert(Assigned(BorlandIDEServices));
  NTAServices := BorlandIDEServices as INTAServices;

  Assert(Assigned(NTAServices));
  Result := NTAServices.ActionList;

  Assert(Assigned(Result));
end;

I leave the rest (enumerating the list and change the shortcut of the action as an exercise to the reader. 😉

  • Thanks 1

Share this post


Link to post
5 hours ago, dummzeuch said:

Ctrl+D is listed in the GExperts Keyboard Shortcuts expert as assigned to the action actFormatSource

 

I saw that, I just couldn't get to anywhere from there. Thank you very much for putting me in the right track.

 

Best,

Carlos

 

Share this post


Link to post

Use GExperts to define an alternate keybinding for this rather go through the rigmarole of creating an expert to do this.

Share this post


Link to post
28 minutes ago, David Hoyle said:

Use GExperts to define an alternate keybinding for this rather go through the rigmarole of creating an expert to do this.

That was my first thought too, but unfortunately there is no main menu entry for it, and GExperts only supports man menu entries to assign keyboard shortcuts.

Share this post


Link to post

I already do this with GExperts so I know it can be done unfortunately I'm on my work machine so cannot show you. The menu item is now hidden underneath View | Editor I think but it is there somewhere. I use Ctrl+Alt+D so I need to disable this shortcut first (disassembly view I think) and then I can assign it for Format Source.

Share this post


Link to post
20 minutes ago, David Hoyle said:

I already do this with GExperts so I know it can be done unfortunately I'm on my work machine so cannot show you. The menu item is now hidden underneath View | Editor I think but it is there somewhere. I use Ctrl+Alt+D so I need to disable this shortcut first (disassembly view I think) and then I can assign it for Format Source.

You are right. It's there in Delphi 10.4. I was looking in Delphi 10.2, where it isn't^D^D^D^D^Dis also right there in that same menu.

 

But: Even in Delphi 10.4, if I remove the shortcut from the main menu entry with GExperts, it still gets shown in the editor's popup menu. And pressing Ctrl+D still calls the formatter.

Edited by dummzeuch

Share this post


Link to post

Ctrl+D has never worked for me but then again I'm still using WordStar/Classic IDE key bindings which might grab Ctrl+D for moving the cursor.

Share this post


Link to post
5 hours ago, David Hoyle said:

Use GExperts to define an alternate keybinding for this rather go through the rigmarole of creating an expert to do this.

Thank you very much for your answer. I already had an expert in place to change the editor's toggle insert / overwrite mode for I am a lousy typist and was going crazy hitting the insert key when home was intended. As for a hammer everything looks like a nail, I thought that was the way to go.

 

 

13 hours ago, dummzeuch said:

I leave the rest (enumerating the list and change the shortcut of the action as an exercise to the reader. 😉

This is what I did:

 

procedure TMelhoriasEditor.ReassignFormatSource;
var
  LNTAServices  : INTAServices;
  LActionList   : TContainedActionList ;
  LAction       : TContainedAction ;
  LActionName   : string;
begin

  Assert(Assigned(BorlandIDEServices));

  LNTAServices := BorlandIDEServices as INTAServices;
  Assert(Assigned(LNTAServices));

  LActionList := LNTAServices.ActionList;
  Assert(Assigned(LActionList));

  for LAction in LActionList do begin
    LActionName := LAction.Name;
    if LActionName.Equals('actFormatSource') then begin
      LAction.Shortcut := (vkF or scCtrl or scShift or scAlt);
      Exit;
    end;
  end;

end;

It changed the main menu entry shortcut to the intended Ctrl+Shift+Alt+F key combination, but ir doesn't fire the action. Also, as noted, the local menu entry still shows associated with Ctrl+D and would fire it in case I remove its association with "MatarTecla", a do-nothing method.

 

This leaves wondering if there's a way to get the TKeyBindingProc value other than the way (IOTAKeyboardServices.LookupKeyBinding) I tried.  Or if I just didn't figured out the way to work  it properly. Unfortunately this seems to be way above my league. 

 

Again, than you all for your precious time helping me.

 

-- 

Best,

Carlos

Share this post


Link to post
8 hours ago, David Hoyle said:

Use GExperts to define an alternate keybinding for this rather go through the rigmarole of creating an expert to do this.

I do this to remap IDE Insight (Ctrl + .) since I use Ctrl+. for the procedure list (old habit from coderush days) - however the IDE has a habit of resetting it while I'm working so I find myself having remap it again several times a day. I thought about writing a plugin to just fire a timer to periodically check if it had reset and then change it again 😉

Share this post


Link to post

I think the problem with the Menu Shortcuts expert is, that it only changes the shortcut for the menu item, not that for the associated action. I guess I'll have to look into this a bit further.

 

@Carlos Tré It's odd that changing the action's shortcut does not change the shortcut of the popup menu entry. They both use an action with the same name. Maybe these are two different actions? Not impossible given the mess some of the IDE modules present.

Share this post


Link to post
5 hours ago, dummzeuch said:

@Carlos Tré It's odd that changing the action's shortcut does not change the shortcut of the popup menu entry. They both use an action with the same name. Maybe these are two different actions? Not impossible given the mess some of the IDE modules present.

That's exactly what I think. I'll toy with it a little more over the weekend and see if I make any progress on this.

Share this post


Link to post
On 10/29/2020 at 10:20 PM, Carlos Tré said:

I already had an expert in place to change the editor's toggle insert / overwrite mode for I am a lousy typist and was going crazy hitting the insert key when home was intended.

@Carlos TréVery interesting; I would like to use that expert, too. What's the name of that expert? Or did you write it yourself and are willing to share the code?

Share this post


Link to post
2 hours ago, Achim Kalwa said:

@Carlos TréVery interesting; I would like to use that expert, too. What's the name of that expert? Or did you write it yourself and are willing to share the code?

I wrote it myself a few years ago, based on an article written by Cary Jensen that you can find here. Attached is the code for the expert in its current state, the changing of the format source key assignment is still a work in progress.

 

Editor.zip

  • Like 1
  • Thanks 2

Share this post


Link to post

@Carlos Tré I like the "Insert Row Below" feature very much, but I had to remove the Ctrl+Enter binding because that is already used for "Open File at Cursor", which I use regulary; but keeping Shift+Enter. And I translated strings and method names from Portuguese to English 😉

 

Share this post


Link to post
51 minutes ago, Achim Kalwa said:

@Carlos Tré I like the "Insert Row Below" feature very much, but I had to remove the Ctrl+Enter binding because that is already used for "Open File at Cursor", which I use regulary; but keeping Shift+Enter. And I translated strings and method names from Portuguese to English 😉

 

@Achim KalwaCtrl+Enter is an old habit from the Brief days, way before Delphi entered the scene. I think it needs some tweaking, I don't quite like its behavior on long lines. As it is ver rare and I don't have much free time and lack the expertise and knowledge to work it out, I guess it will stay this way for the moment.

 

I usually turn "English language mode" on to code as it is consistent with the programming language itself, but when I wrote that piece I was particularly mad at unnecessary anglicization that stormed into Brazilian Portuguese, particularly in TI lingo, and overreacted; 🙂

 

Thank you for taking the time to come back and comment, I appreciated it very much.

Share this post


Link to post
Quote

the Ctrl+Enter binding because that is already used for "Open File at Cursor", 

Thats the way I like to use it too.

Unfortunately this is veeeery unreliable in the last versions, so I mainly changed my habits to use right click "Open Files at Cursor",
which is not the same convenience.

I hope one day the LSP will fix all that, and make "Ctrl+Enter for "Open File at Cursor" great again  :classic_smile:

Share this post


Link to post
On 10/30/2020 at 5:08 AM, dummzeuch said:

I think the problem with the Menu Shortcuts expert is, that it only changes the shortcut for the menu item, not that for the associated action. I guess I'll have to look into this a bit further.

 

@Carlos Tré It's odd that changing the action's shortcut does not change the shortcut of the popup menu entry. They both use an action with the same name. Maybe these are two different actions? Not impossible given the mess some of the IDE modules present.

Well, let's add to the confusion a little bit:

 

I left this thing completely alone as I have more pressing matters to address but, much to my surprise, yesterday I went for a Ctrl+Shif+Alt+D attached to a duplicate line command I use all the time, and as I hit Ctrl+Shif+ALt+F by mistake I noticed it had started working as expected at some point. I can't make any sense of it, Ctrl-D is still listed as the context menu hot key, and the only things in between are some windows updates ans restarts, and the latest IDE patch that, I guess, is not related at all. Go figure!

 

This late reply is meant only as a feedback to those who kindly dedicated some time to help and educate me.

 

Once again, thank you all very much.

 

-- Carlos

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×