Lazarus

Programming => General => Topic started by: yurkad on January 18, 2020, 11:16:51 pm

Title: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 18, 2020, 11:16:51 pm
We assume that we have an IDE menu item with KeyToShortCut (VK_C, [ssShift, ssCtrl]).

How to invoke this point programmatically?

Thanks!
Title: Re: How to invoke Ide Menu item programmatically?
Post by: winni on January 19, 2020, 12:52:29 am
Hi!

Have a look at the unit mouseandkeyinput in the lazarus components  directory.

Winni
Title: Re: How to invoke Ide Menu item programmatically?
Post by: GetMem on January 19, 2020, 07:47:50 am
@yurkad

I assume your code is in a package, and that package was added to IDE, like in your previous thread(https://forum.lazarus.freepascal.org/index.php/topic,48089.0.html):
Code: Pascal  [Select][+][-]
  1. uses IDECommands, LCLType;
  2. var
  3.   Key: Word;
  4. begin
  5.   Key := VK_C;
  6.   ExecuteIDEShortCut(Self, Key, [ssShift, ssCtrl]);    
  7. end;
Title: Re: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 19, 2020, 04:40:41 pm
@winni, thanks!

@GetMem.

Thank you very much. Yes it is your sample.

Based on your example I solved my needs. But there were two things missing:

1. Invoke Ide menu point - now is fine - thanks again.

2. My new package has an old name: projectstatistics and only one projectstatisticsintf.pas file.

For two days I tried to change the name package for mine - 'GetIde', and I couldn't.
Nor could I create a new package with a new name. That's why I used your package name and the content - mine.

If you can, please help me on this too - How to change name of package.

Thank you!
Title: Re: How to invoke Ide Menu item programmatically?
Post by: Martin_fr on January 19, 2020, 04:55:54 pm
You may want to try
Code: Pascal  [Select][+][-]
  1.   boolSuccess := ExecuteIDECommand(nil, ecCompleteCode);

Then it will work, even if the keymap gets changed.
Title: Re: How to invoke Ide Menu item programmatically?
Post by: GetMem on January 19, 2020, 07:28:32 pm
@yurkad

1. Make backup first
2. Open package projectstatistics.lpk
3. Go to More-->Save package As...(see attached image)
4. For consistency, you should also rename projectstatisticsintf.pas(unit underlined with red in the attached image) to GetIDEIntf.pas
Title: Re: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 19, 2020, 11:44:56 pm
@Martin_fr.
Thank, but I don't know how to use this. Maybe I will learn.

@GetMem.
Thank you.
Really it is easy (when there is explanation).
Title: Re: How to invoke Ide Menu item programmatically?
Post by: Martin_fr on January 20, 2020, 12:20:09 am
@Martin_fr.
Thank, but I don't know how to use this. Maybe I will learn.

First: I haven't tested it, but it should be fine....

You use it, like "  ExecuteIDEShortCut(Self, Key, [ssShift, ssCtrl]);"

It is defined in the same unit.
"ecCodeCompletion" is in unit KeyMapping. So you can search for the default keycombo there, and find the "ec....." constant.


--
"ec" stands for EditorCommand.

Almost every action you can perform in the Editor has an "ec" constant.
Title: Re: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 20, 2020, 01:40:35 am
@Martin_fr.

From what I read it must be easy.

I am going to try.

Now I realize that I must learn everything from unit IDECommands.

By the way: command ExecuteIDEShortCut gives a Sissegv error. But I still don't ask for help - maybe I have a lot of mistakes on the issue of packages - it's new to me.
I will try to fix.

Thanks!
Title: Re: How to invoke Ide Menu item programmatically?
Post by: Martin_fr on January 20, 2020, 02:45:51 am
Btw, you can debug the entire IDE  (with your package).

Open one IDE, then open project "ide/lazarus.lpi"

Note that this project does not re-build the exe. So you must make sure that you do Tools->Build Lazarus
(and ensure it is linking a lazarus.exe, not a lazarus.old.exe. Check in your install folder. The "old" will happen, if the IDE is still locked. Happens if gdb crashes)

Once build, you can use F9 to run the IDE project, and a 2nd IDE opens that you can debug.



IF you search the components folder for ExecuteIDECommand, you will find several examples.
Title: Re: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 20, 2020, 01:21:23 pm
@Martin_fr,

Thanks by helpful tips!
Title: Re: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 26, 2020, 02:49:39 pm
Trying to solve this question.

In Ide there is a menu item
Menu->Project->Project Options... Shift+Ctrl+F11.

This point works by using mouse or using keyboard (Shift + Ctrl + F11).
I tried to invoke this point with the following code:

Code: Pascal  [Select][+][-]
  1. uses IDECommands, LCLType;
  2. var
  3.   Key: Word;
  4. begin
  5.   Key := VK_11;
  6.   ExecuteIDEShortCut(Self, Key, [ssShift, ssCtrl]);    
  7. end;

Command ExecuteIDEShortCut gives a SIGSEGV error.

Mi OS is Windows 10.
Lazarus Version: 2.0.6
FPC Version: 3.0.4
SVN Revision: 62129.

I was also trying to use package
lazmouseandkeyinput

The code:
Code: Pascal  [Select][+][-]
  1.   KeyInput.Apply([ssShift]);
  2.   KeyInput.Apply([ssCtrl]);
  3.   KeyInput.Press(VK_F11);            
  4.   KeyInput.Unapply([ssShift]);
  5.   KeyInput.Unapply([ssCtrl]);

It has no effect.

Please, any advice?
Title: Re: How to invoke Ide Menu item programmatically?
Post by: howardpc on January 26, 2020, 03:11:54 pm
Contributors so far assumed you are writing some sort of IDE extension, which is compiled into a running IDE instance.
Are you trying to invoke an IDE command from outside the IDE, as part of an independent project?
Title: Re: How to invoke Ide Menu item programmatically?
Post by: Martin_fr on January 26, 2020, 03:57:51 pm
Indeed ExecuteIDEShortCut is not meant for opening the options dialog.
ExecuteIDEShortCut takes the keystroke, and translates it into an ec.... command, according to the current keysettings.
There is no ec<OpenOptions> command. So it can not do that.

However it should not crash, it should just do nothing.

As long as your package is installed into the IDE, running inside the IDE, and you are not doing this during unit initialization (wait at least until "Register" was called), you might be able to go via the MenuIntf unit
itmOptionsDialogs is a menu section, so you can iterate through its entries to find the one to open the dialog.

Or better you can use LazIDEIntf: DoOpenIDEOptions.


You will have to crawl a bit through the code to find out how to do this.
- browse the IDEInf package
- browse the unit "main" and search for related terms
- find existing packages that do what you need

If you can put your findings on the wiki, and start/extend the docs


Title: Re: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 26, 2020, 04:25:07 pm
@howardpc.

Thanks by answer.
Yes, I am trying to invoke an IDE command from outside the IDE, as part of an independent project.

@Martin_fr.

Thank you. I understood.

By both answers two questions appear:

1.
How to invoke a package without using of Menu Point and without using of IDEShortCut?

2.
How to invoke a package automatically after starting IDE?

Many thanks!

Title: Re: How to invoke Ide Menu item programmatically?
Post by: Martin_fr on January 26, 2020, 04:36:37 pm
If you want to control anything in the IDE, but do so from an independent stand alone project, then I can only think of mouse and key input.
I haven't done much with that package, so I can't advice further on it.
Mouse and Key, relies on the target IDE config. I.e. if you send a key, but the user changed the keymap....

MS Windows also has some API, where you can access TWinControls of another app. Some GetWindow function, and you can query the caption, send keys, ..... But that is from very distant memory too, so again for your own research (and not sure what is on *nix).

The IDE has no remote API, but you could write a  package, that gets installed in the IDE and takes remote commands.
Title: Re: How to invoke Ide Menu item programmatically?
Post by: Martin_fr on January 26, 2020, 04:39:37 pm
1.
How to invoke a package without using of Menu Point and without using of IDEShortCut?

2.
How to invoke a package automatically after starting IDE?

If that is inside the IDE.

2) If that package has a register procedure (and you marked the unit in the package window) then that gets called by the IDE.

Open the package, select a unit, toggle "Register procedure"
And make sure you have a "procedure register" in the interface of that unit.


1) Invoked by what?
By another package?
By the user via keystroke?
By external app?
Title: Re: How to invoke Ide Menu item programmatically?
Post by: yurkad on January 26, 2020, 07:06:21 pm
@Martin_fr.

Thanks by answer.

1) Invoked by what?

By an independent project from outside the IDE.

TinyPortal © 2005-2018