Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SendKeys or Keystrokes Using iLogic

18 REPLIES 18
Reply
Message 1 of 19
mehatfie
4904 Views, 18 Replies

SendKeys or Keystrokes Using iLogic

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

 

18 REPLIES 18
Message 2 of 19
Yijiang.Cai
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.
Email: River-Yijiang.Cai@autodesk.com
Message 3 of 19
mehatfie
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

Message 4 of 19
mehatfie
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

Message 5 of 19
MjDeck
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
Autodesk, Inc.

Message 6 of 19
mehatfie
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

 

 

Message 7 of 19
MjDeck
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
Autodesk, Inc.

Message 8 of 19
mehatfie
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

Message 9 of 19
MjDeck
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
Autodesk, Inc.

Message 10 of 19
mehatfie
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

Message 11 of 19
mehatfie
in reply to: mehatfie

Annnnnnnd there's the road block.....

 

Using the methods you told me I was able to do exactly wat I needed and open the dialog box. This will work great for my simpler models.

 

Unfortunately, the dialog box pops up for the active document on screen only it seems. So with my assembly which uses various iLogic components and pushes dimensions given by a user down to each component, if I run this rule it will keep open the dialog box and supplying the information only for the assembly itself, while each component needs its seperate properties.

 

MY QUESTION IS:   Is there a way to open a component using iLogic from an assembly?

 

I attempted a few different things.

 

- I created a text parameter inside the component and used "ThisDoc.PathAndFile(False)" to get it's information. Then went to the Assembly and grabbed this parameter, attempting to launch it using "ThisDoc.Launch("path/file.ext")"  which I now realize is most likely for non-inventor documents

 

I tried this and a few other things just to see if I could figure it out by myself, but unfortunately I could not.

 

Thanks again for all your help

Mitch

 

 

Message 12 of 19
mehatfie
in reply to: mehatfie

Sorry, I've figured it out if anybody is interested and needs this later on. My users can change the file names of the documents and the links still stay intact through the "Copy Design" tool in The Vault

 

Which is why I need to get the filename directly from the component...

 

 

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

fname=Parameter("Outlet_Flange:1", "DocumentName")&".ipt"
doc=ThisApplication.Documents.Open(fname)

 

Cheers

Mitch

Message 13 of 19
MjDeck
in reply to: mehatfie

You can do it with the following API code:
ThisApplication.Documents.Open(fullDocumentPathName, True)
I think that will also make it the active document.

If the document is a subassembly, you might want to open it in a specific level of detail.  To do that, add the level of detail name to the end of the path name, like this:
ThisApplication.Documents.Open("C:\Models\SubAssemblyA<LevelofDetail1>", True)


Mike Deck
Software Developer
Autodesk, Inc.

Message 14 of 19

Hi mhatfield,

 

Here's another example that demonstrates how to set an iproperty for each part in an assembly, just in case it comes in handy for your overall task.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

'Define the open document
Dim openDoc As Document
openDoc = ThisDoc.Document
Dim docFile As Document 

'kAssemblyDocumentObject = 12291 
If openDoc.DocumentType = 12291 Then

	'Iterate though the part files in the assembly
	For Each docFile In openDoc.AllReferencedDocuments
	
		'kPartDocumentObject = 12290 
		If docFile.DocumentType = 12290 Then
		
		'format  file name		
		Dim FNamePos As Long
            	FNamePos = InStrRev(docFile.FullFileName, "\", -1)	
	         
            	Dim docFName As String
		docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
			'toggle description value
			If iProperties.Value(docFName, "Project", "Description") <> "On"  Then
			'set description
			iProperties.Value(docFName, "Project", "Description") = "On"
			else if iProperties.Value(docFName, "Project", "Description") = "On" Then
			'set description
			iProperties.Value(docFName, "Project", "Description") = "Off"	
			End If			
		End If 
	Next	
	Else
	MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!")
End If 

 

Message 15 of 19
MjDeck
in reply to: Curtis_Waguespack

Curtis,
 Instead of using the numeric constants, you can use the enum definitions by adding the type name:
If openDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject 


Also, this will work in iLogic and VB.NET code:
docFName = IO.Path.GetFileName(docFile.FullFileName)


Mike Deck
Software Developer
Autodesk, Inc.

Message 16 of 19
Curtis_Waguespack
in reply to: MjDeck

Message 17 of 19

Thank you Mike and Curtis!

 

The active document code you provided did work with completing the task as well, and probably is a better idea, as then I know the add-in will recognize which document to open for, rather then opening prematurly for the assembly if the specific part has not opened yet.

 

That was going to be my next task Curtis, your a mind reader! Very helpful information. I've almost worked out all of the kinks in the model itself, and am moving on to the form and drawing soon. This iProperties code will definitly be used!

 

Thanks again for all your help

Mitch

 

 

Message 18 of 19
jdkriek
in reply to: Yijiang.Cai

Yijiang,

 

Why wouldn't you just use SendKeys? Cat Frustrated

 

Public Sub TestSendKeys()
SendKeys "{e}"
End Sub

 


@Yijiang.Cai/Autodesk wrote:

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


 

Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


Message 19 of 19
ACEDeSmedt
in reply to: jdkriek

I run SendKeys with the Following setup:

 

- Windows 7  bit

- Inventor 2012  bit

 

with VBA and I get the error: 70 - Permission denied.

=================================
If this is the solution, push the solution button 😉 (and maybe some kudos)
Autodesk Product Design Suit - Ultimate edition (Subscription)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report