Get ShiftStateEnum during a Select Event

Get ShiftStateEnum during a Select Event

pmartick
Advocate Advocate
319 Views
3 Replies
Message 1 of 4

Get ShiftStateEnum during a Select Event

pmartick
Advocate
Advocate

Inventor 2021, latest version:

 

Here's a sample of a rule I'm working on.  When a user selects an occurrence an assembly, I want something different to happen if they select it while holding down Shift.  I've tried just tracking this with an If Then statement controlling a boolean in a Mouse Event subroutine, but this doesn't seem to take effect during a Select event.

 

In this sample rule, select an assembly occurrence with either the shift key pressed or not and a messagebox should confirm it if there's a way to make it work, otherwise press Esc to exit the rule:

 

Class ThisRule

	Dim bStop As Boolean = False
	Dim bShift As Boolean = False
	Dim oApp As Inventor.Application
	Dim oDoc As Document
	Dim oComMgr As CommandManager
	Private WithEvents evInteractionEvents As InteractionEvents
	Private WithEvents evSelectEvents As SelectEvents
	Private WithEvents evKeyboardEvents As KeyboardEvents
	Private WithEvents evMouseEvents As MouseEvents

	Sub Main
		oApp = ThisApplication
		oDoc = ThisDoc.Document
		If TypeOf oDoc Is AssemblyDocument And Right(oApp.UserInterfaceManager.ActiveEnvironment.DisplayName, 8) = "Assembly" Then
			oComMgr = oApp.CommandManager
			evInteractionEvents = oComMgr.CreateInteractionEvents
			evSelectEvents = evInteractionEvents.SelectEvents
			evSelectEvents.AddSelectionFilter(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter)
			evSelectEvents.WindowSelectEnabled = False
			evSelectEvents.Enabled = True
			evInteractionEvents.Start
			Do
				oApp.UserInterfaceManager.DoEvents
			Loop Until bStop = True
			evInteractionEvents.Stop
		Else
			MessageBox.Show("This rule must be run in an Assembly Environment.")
		End If
	End Sub

	Sub evKeyboardEvents_OnKeyPress(lKey As Long) Handles evKeyboardEvents.OnKeyPress
		If lKey = 27 Then bStop = True
	End Sub

	Sub evSelectEvents_OnPreSelect(ByRef PreSelectEntity As Object, ByRef DoHighlight As Boolean, ByRef MorePreSelectEntities As ObjectCollection, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As Inventor.View) Handles evSelectEvents.OnPreSelect
		PreSelectEntity = PreSelectEntity.OccurrencePath.Item(1)
		DoHighlight = True
	End Sub

	Sub evMouseEvents_OnMouseClick(ByVal Button As MouseButtonEnum, ByVal ShiftKeys As ShiftStateEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As Inventor.View) Handles evMouseEvents.OnMouseClick
		If ShiftKeys = ShiftStateEnum.kShiftStateShift Then
			bShift = True
		Else
			bShift = False
		End If
	End Sub

	Sub evSelectEvents_OnSelect(ByVal JustSelectedEntities As ObjectsEnumerator, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As Inventor.View) Handles evSelectEvents.OnSelect
		oEntity = JustSelectedEntities.Item(1)
		If Shift = True Then
			MessageBox.Show("Shift Key")
		Else
			MessageBox.Show("No Shift Key")
		End If
	End Sub

End Class

Thanks for your help. 

0 Likes
320 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor

There is a simpler solution that might work in your situation. If you ask the users to select (with the shift button) multiple occurrences and then start the rule. Then you can use the selection set to get all selected occurrences. something like this:

Dim doc As AssemblyDocument = ThisDoc.Document

Dim occs As List(Of ComponentOccurrence) = doc.SelectSet.OfType(Of ComponentOccurrence).ToList()

If (occs.Count = 0) Then
    MsgBox("Nothing was selected!")
Else
    For Each occ As ComponentOccurrence In occs
        MsgBox("The folloing occurrences was selected: " & occ.Name)
    Next
End If

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 4

Michael.Navara
Advisor
Advisor

I don't know your use case, but if it is possible, you can use MouseClick event instead of OnSelect event.

Solution for double click on sketched symbol is discussed here.

The solution for component occurrence is very similar.

0 Likes
Message 4 of 4

g.georgiades
Advocate
Advocate

Hi,

 

Checking if My.Computer.Keyboard.ShiftKeyDown is true is a quick solution that is not tied/limited to inventor or its events.

 

You can also check if control and/or alt keys are pressed or send keystrokes.

 

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/objects/my-computer-keyboard...

https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.devices.keyboard?view=windowsdeskt...

 

(Note the "My" intellisense may not work in the iLogic editor)

 

If you need more granularity e.g. which side was pressed, then you may need to use windows api hooks.