Inventor General Discussion

Inventor General Discussion

Reply
Mentor
mehatfie
Posts: 163
Registered: ‎02-10-2012
Message 1 of 19 (1,500 Views)

SendKeys or Keystrokes Using iLogic

1500 Views, 18 Replies
02-24-2012 11:30 AM

Hi All,



I've looked around and done some research on this and it seems possible just I can't figure out how to do it.



I'm looking to send keystrokes programmically using iLogic as if the keys were actually being pressed. I just want to send a simple set of keys like "QQ".



Does anybody know how I would do this through iLogic?



Even if it is an improper method of programming, is there anyone who can tell me how to do this



Thanks

Mitch

 

Employee
Yijiang.Cai
Posts: 358
Registered: ‎05-20-2008
Message 2 of 19 (1,471 Views)

Re: SendKeys or Keystrokes Using iLogic

02-27-2012 09:26 PM in reply to: mehatfie

Please create one part document first. When copying the code lines below in VBA Editor dialog and running macro, it will work fine, and this simulates to press the "E" key to create extrusion.

 

Thanks,
River

 

Option Explicit

 

Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_KEYDOWN As Long = &H100
Private Const WM_KEYUP = &H101

 

Sub Main()
    Dim FT_hWnd As Long
    FT_hWnd = FindWindow(vbNullString, ThisApplication.Caption)

    Call PostMessage(FT_hWnd, WM_KEYDOWN, Asc("E"), 0&)
    Call PostMessage(FT_hWnd, WM_KEYUP, Asc("E"), 0&)
End Sub

Thanks,
River Cai

Inventor Quality Assurance Team
Autodesk, Inc.
Mentor
mehatfie
Posts: 163
Registered: ‎02-10-2012
Message 3 of 19 (1,458 Views)

Re: SendKeys or Keystrokes Using iLogic

02-28-2012 06:11 AM in reply to: Yijiang.Cai

Wow, thank you very much!

 

I've been researching and researching on how to do this and have never come close to anything that looks like this.

 

Thanks again

Mitch

Mentor
mehatfie
Posts: 163
Registered: ‎02-10-2012
Message 4 of 19 (1,452 Views)

Re: SendKeys or Keystrokes Using iLogic

02-28-2012 11:03 AM in reply to: Yijiang.Cai

Hi River,

 

The code worked great when I manually go in and run the macro. In the end, I would like to activate it and run it through an iLogic Rule so I can control when to use it.

 

I assumed it was as simple as using the snippet

 

Format:HTML Format Version:1.0 StartHTML:     165 EndHTML:    1121 StartFragment:     314 EndFragment:    1089 StartSelection: 314 EndSelection:     314
SyntaxEditor Code Snippet

InventorVb.RunMacro("DocumentProject", "Module1", "Main")

 

But it doesn't seem to be working

 

Thanks

Mitch

Employee
MjDeck
Posts: 959
Registered: ‎02-24-2009
Message 5 of 19 (1,444 Views)

Re: SendKeys or Keystrokes Using iLogic

02-28-2012 07:25 PM in reply to: mehatfie

Mitch,

 You can use Windows (Win32) code directly in an iLogic rule, instead of running a VBA macro.  See the attached rule.  I think if you added the call to SetFocus in the VBA code, the VBA macro would also work from an iLogic rule. 
However, this rule won't work if it is triggered to run by a parameter change that is made in the Parameters dialog.
If you want to send a key to start an Inventor command, you can do it more easily with the Inventor API call ControlDefinition.Execute or ControlDefinition.Execute2.



Mike Deck
Software Developer
DLS - Mechanical Design
Autodesk, Inc.

Mentor
mehatfie
Posts: 163
Registered: ‎02-10-2012
Message 6 of 19 (1,424 Views)

Re: SendKeys or Keystrokes Using iLogic

02-29-2012 06:39 AM in reply to: MjDeck

Thanks Mike!

 

I attempted to post the previous code directly into iLogic but it did not work. The only issue with the code you've provided is that it can only use one character. A little fiddling around with it let me find out that if I use the "PostMessage" line twice, it will use two characters.

 

I'm new to programming and unsure where I would use the ControlDefinition.Execute command. But the code you provided does work! Thank You!

 

Unfortunately I have but one more question, I thought I knew how to do this but apparently I do not.

 

Is it possible to access the "Customize" dialog box through code and change the shortcut keys? I would like to do this as a check and to insure that the correct keys I input to the code are indeed what the user has set. Or, save the string they have, and input the characters I desire, then re-input the previous shortcut that the user originally had.

 

Just for clarity, this is (as far as I know how to access it)  Tools Tab > Customize > Keyboard Tab >... And then the command I'm trying to access, for helpfulness, lets say I'm trying to change the Command Name: "Constraint..." from Keys: "C" to Keys: "PP"

 

Thanks Again

Mitch

 

 

Employee
MjDeck
Posts: 959
Registered: ‎02-24-2009
Message 7 of 19 (1,417 Views)

Re: SendKeys or Keystrokes Using iLogic

02-29-2012 12:36 PM in reply to: mehatfie

Mitch,
 Instead of working with the particular command shortcut keys, it's better to start a command with code like this:


Dim oControlDef As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyInsertConstraintCmd")
oControlDef.Execute()

That will start the Constraint command.
You can get the shortcut keys for a command: it's available as the DefaultShortcut or OverrideShortcut property on the ControlDefinition object.  But if you have a ControlDefinition object (as in the code above), you might as well just Execute it instead of worrying about the keys.
  You need to know the internal name of the command.  I attached a list of available command names.



Mike Deck
Software Developer
DLS - Mechanical Design
Autodesk, Inc.

Mentor
mehatfie
Posts: 163
Registered: ‎02-10-2012
Message 8 of 19 (1,415 Views)

Re: SendKeys or Keystrokes Using iLogic

02-29-2012 12:58 PM in reply to: MjDeck

Hi Mike,

 

The document you provided will definitly help for future coding as I'm learning how to do so. Thank you very much for providing me with this.

 

Unfortunately, where my issue lies is that I am working with an Add-In from an outside source. I will try to see if I can find the "internal name" given for this, but that is the reason why I am trying to use keys to open the dialog box. I have done quite a bit of research and found that I need to reference the .dll (which I found) as well as use the dialog box file (which I did not find).

 

So my next thought is to use the shortcut keys to open the dialog box as you can customize this command

 

Thanks

Mitch

Employee
MjDeck
Posts: 959
Registered: ‎02-24-2009
Message 9 of 19 (1,412 Views)

Re: SendKeys or Keystrokes Using iLogic

02-29-2012 01:09 PM in reply to: mehatfie

Here is a rule that will list the internal command name for all add-in commands.  It will save the list to C:\InventorCommands.txt.  You should be able to find your command in there.



Mike Deck
Software Developer
DLS - Mechanical Design
Autodesk, Inc.

Mentor
mehatfie
Posts: 163
Registered: ‎02-10-2012
Message 10 of 19 (1,405 Views)

Re: SendKeys or Keystrokes Using iLogic

02-29-2012 01:30 PM in reply to: MjDeck

Wow, you just did 2 weeks worth of researching and digging in 5 seconds with those codes...

 

Thank you very much Mike! Five steps closer to model and drawing automation!

 

Thanks Again

Mitch

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.