.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Catching Keyboard and Mouse Events

14 REPLIES 14
SOLVED
Reply
Message 1 of 15
Anonymous
8466 Views, 14 Replies

Catching Keyboard and Mouse Events

Anonymous
Not applicable

Right now I am poking around to see whether there is possibility to catch events like KeyDown or MouseWheel.

 

I assume they would be available either on the Editor (AutoDesk.AutoCAD.EditorInput.Editor) object or somewhere under the Application (AutoDesk.AutoCAD.ApplicationServices.Application). I saw event handlers like Editor.Rollover but attaching to it didn't bring me far.

 

Has anyone a clue to where to look? Thanks!

0 Likes

Catching Keyboard and Mouse Events

Right now I am poking around to see whether there is possibility to catch events like KeyDown or MouseWheel.

 

I assume they would be available either on the Editor (AutoDesk.AutoCAD.EditorInput.Editor) object or somewhere under the Application (AutoDesk.AutoCAD.ApplicationServices.Application). I saw event handlers like Editor.Rollover but attaching to it didn't bring me far.

 

Has anyone a clue to where to look? Thanks!

14 REPLIES 14
Message 2 of 15
DiningPhilosopher
in reply to: Anonymous

DiningPhilosopher
Collaborator
Collaborator

There aren't any events that that correspond to keyboard and mouse input. 

 

If you can say more about what you're trying to accomplish, there may be other ways.

There aren't any events that that correspond to keyboard and mouse input. 

 

If you can say more about what you're trying to accomplish, there may be other ways.

Message 3 of 15
Anonymous
in reply to: DiningPhilosopher

Anonymous
Not applicable

Right... I had hoped I could use some free time to produce a command that would run pacman in the current document. Perhaps that's not entirely impossible but I'll leave it for some other time.

 

Particularly, I thought a nice-to-have is a layer walker, program logic that keeps only one layer turned on at a time, and uses a combination like ctrl+mouse wheel to run up and down through the list of layers.

 

From what I have seen, this might be possible to some extent through custom keyboard shortcuts but I wouldn't go there before being sure there is nothnig that can be done from ObjectARX' side.

0 Likes

Right... I had hoped I could use some free time to produce a command that would run pacman in the current document. Perhaps that's not entirely impossible but I'll leave it for some other time.

 

Particularly, I thought a nice-to-have is a layer walker, program logic that keeps only one layer turned on at a time, and uses a combination like ctrl+mouse wheel to run up and down through the list of layers.

 

From what I have seen, this might be possible to some extent through custom keyboard shortcuts but I wouldn't go there before being sure there is nothnig that can be done from ObjectARX' side.

Message 4 of 15

BlackBox_
Advisor
Advisor

@DiningPhilosopher wrote:

There aren't any events that that correspond to keyboard and mouse input. 


Forgive my curiosity, but while this is no Ctrl+MouseWheel Event handler, what's wrong with DoEvents(), and acedUsrBrk() to catch user input?

 

Quote:

 

"It's fairly common for developers to want to check for user input from time to time during long operations, especially to see whether the user wants to cancel the current activity. In VB you'd use DoEvents() to enable messages to be processed by the application's message loop and in ObjectARX you'd use acedUsrBrk().

So how to do this in .NET?

 

The answer is to use a message filter. This allows us to check on user-input events... we still call DoEvents, as with previous versions of VB, which allows user input events (such as keystrokes) to flow into our message filter function. We can then detect the events we care about, and filter them out, if necessary."

 

http://through-the-interface.typepad.com/through_the_interface/2007/02/index.html



"How we think determines what we do, and what we do determines what we get."


@DiningPhilosopher wrote:

There aren't any events that that correspond to keyboard and mouse input. 


Forgive my curiosity, but while this is no Ctrl+MouseWheel Event handler, what's wrong with DoEvents(), and acedUsrBrk() to catch user input?

 

Quote:

 

"It's fairly common for developers to want to check for user input from time to time during long operations, especially to see whether the user wants to cancel the current activity. In VB you'd use DoEvents() to enable messages to be processed by the application's message loop and in ObjectARX you'd use acedUsrBrk().

So how to do this in .NET?

 

The answer is to use a message filter. This allows us to check on user-input events... we still call DoEvents, as with previous versions of VB, which allows user input events (such as keystrokes) to flow into our message filter function. We can then detect the events we care about, and filter them out, if necessary."

 

http://through-the-interface.typepad.com/through_the_interface/2007/02/index.html



"How we think determines what we do, and what we do determines what we get."

Message 5 of 15
DiningPhilosopher
in reply to: Anonymous

DiningPhilosopher
Collaborator
Collaborator

You can use an IMessgeFilter to intercept windows messages, including mouse and keyboard events, but doing that continuously, as opposed to only doing some custom command would be extremely probelmatic since you can't tell what may be happening in AutoCAD from a windows message (for example, is there a dialog active, or is the user editing text, or is the user using the Properties Palette, etc.). so it would depend on if you wanted to do it during your custom command, or if you wanted to watch messages continuously while the user works in AutoCAD.

 

If it is the latter, see BlackBoxCadd's reply for a link to Kean's blog post showing how the IMessageFilter works.

0 Likes

You can use an IMessgeFilter to intercept windows messages, including mouse and keyboard events, but doing that continuously, as opposed to only doing some custom command would be extremely probelmatic since you can't tell what may be happening in AutoCAD from a windows message (for example, is there a dialog active, or is the user editing text, or is the user using the Properties Palette, etc.). so it would depend on if you wanted to do it during your custom command, or if you wanted to watch messages continuously while the user works in AutoCAD.

 

If it is the latter, see BlackBoxCadd's reply for a link to Kean's blog post showing how the IMessageFilter works.

Message 6 of 15

DiningPhilosopher
Collaborator
Collaborator

@BlackBoxCAD wrote:

@DiningPhilosopher wrote:

There aren't any events that that correspond to keyboard and mouse input. 


Forgive my curiosity, but while this is no Ctrl+MouseWheel Event handler, what's wrong with DoEvents(), and acedUsrBrk() to catch user input?

 

Quote:

 

"It's fairly common for developers to want to check for user input from time to time during long operations, especially to see whether the user wants to cancel the current activity. In VB you'd use DoEvents() to enable messages to be processed by the application's message loop and in ObjectARX you'd use acedUsrBrk().

So how to do this in .NET?

 

The answer is to use a message filter. This allows us to check on user-input events... we still call DoEvents, as with previous versions of VB, which allows user input events (such as keystrokes) to flow into our message filter function. We can then detect the events we care about, and filter them out, if necessary."

 

http://through-the-interface.typepad.com/through_the_interface/2007/02/index.html


What's wrong with it depends on whether the need is continuous or if it is transient (e.g., only while a certain command is running). 

 

That's why I asked the OP for more detail before going further..

0 Likes


@BlackBoxCAD wrote:

@DiningPhilosopher wrote:

There aren't any events that that correspond to keyboard and mouse input. 


Forgive my curiosity, but while this is no Ctrl+MouseWheel Event handler, what's wrong with DoEvents(), and acedUsrBrk() to catch user input?

 

Quote:

 

"It's fairly common for developers to want to check for user input from time to time during long operations, especially to see whether the user wants to cancel the current activity. In VB you'd use DoEvents() to enable messages to be processed by the application's message loop and in ObjectARX you'd use acedUsrBrk().

So how to do this in .NET?

 

The answer is to use a message filter. This allows us to check on user-input events... we still call DoEvents, as with previous versions of VB, which allows user input events (such as keystrokes) to flow into our message filter function. We can then detect the events we care about, and filter them out, if necessary."

 

http://through-the-interface.typepad.com/through_the_interface/2007/02/index.html


What's wrong with it depends on whether the need is continuous or if it is transient (e.g., only while a certain command is running). 

 

That's why I asked the OP for more detail before going further..

Message 7 of 15
Anonymous
in reply to: DiningPhilosopher

Anonymous
Not applicable

Ok, I'll still have to go back to what BlackBoxCAD proposed.

 

I would like to have that continously present, but what I have come up with is that a command or a toolbar button can be created, that opens a modeless window. Then, this window can capture mouse wheel events and switch layers, regen the drawing...

 

If one wants to play ninja and hide this window from users, perhaps there is a way to make this window invisible and to make it follow the cursor until the first next click closes it. This would provide illusion that there is a command that does this.

 

But, otherwise, the window could be operating cross document and cross command until it is closed.

0 Likes

Ok, I'll still have to go back to what BlackBoxCAD proposed.

 

I would like to have that continously present, but what I have come up with is that a command or a toolbar button can be created, that opens a modeless window. Then, this window can capture mouse wheel events and switch layers, regen the drawing...

 

If one wants to play ninja and hide this window from users, perhaps there is a way to make this window invisible and to make it follow the cursor until the first next click closes it. This would provide illusion that there is a command that does this.

 

But, otherwise, the window could be operating cross document and cross command until it is closed.

Message 8 of 15

BlackBox_
Advisor
Advisor

My post was not in any way intended to challenge the request for more information; instead it was simply in the hopes of gaining some level of understanding behind the seemingly absolute response you originally offered.

 

You seem to possess a vast knowledge of the .NET API, and I just wanted to express my appreciation for your time in clarifying the topic further.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

0 Likes

My post was not in any way intended to challenge the request for more information; instead it was simply in the hopes of gaining some level of understanding behind the seemingly absolute response you originally offered.

 

You seem to possess a vast knowledge of the .NET API, and I just wanted to express my appreciation for your time in clarifying the topic further.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 9 of 15
BlackBox_
in reply to: BlackBox_

BlackBox_
Advisor
Advisor

"You seem to possess a vast knowledge of the .NET API, and I just wanted to express my appreciation for your time in clarifying the topic further."

 

... I thought I recognized you Tony (DiningPhilosopher, TT, TheMaster), by the accuracy, and completeness of the responses posted, which are always helpful, if not educational.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

0 Likes

"You seem to possess a vast knowledge of the .NET API, and I just wanted to express my appreciation for your time in clarifying the topic further."

 

... I thought I recognized you Tony (DiningPhilosopher, TT, TheMaster), by the accuracy, and completeness of the responses posted, which are always helpful, if not educational.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 10 of 15
DiningPhilosopher
in reply to: Anonymous

DiningPhilosopher
Collaborator
Collaborator

If you use an IMessageFilter and check if certain keys are pressed, and what is currently happening in AutoCAD, you might be able to do what you need.  You can use System.Windows.Forms.ModifierKeys to check if CTR/SHIFT/ALT are pressed when you see a mouse wheel message, but you'd have to find a combination of keys that aren't used by AutoCAD with mouse wheel activity, if you don't want to step on that.

If you use an IMessageFilter and check if certain keys are pressed, and what is currently happening in AutoCAD, you might be able to do what you need.  You can use System.Windows.Forms.ModifierKeys to check if CTR/SHIFT/ALT are pressed when you see a mouse wheel message, but you'd have to find a combination of keys that aren't used by AutoCAD with mouse wheel activity, if you don't want to step on that.

Message 11 of 15

DiningPhilosopher
Collaborator
Collaborator

@BlackBoxCAD wrote:

My post was not in any way intended to challenge the request for more information; instead it was simply in the hopes of gaining some level of understanding behind the seemingly absolute response you originally offered.


The OP asked about events, not hooking windows messages.

 

I asked for more specifics because there are ways to handle things like pointer movements in the drawing editor without having to handle low-level input events.

 

 

 

 

0 Likes


@BlackBoxCAD wrote:

My post was not in any way intended to challenge the request for more information; instead it was simply in the hopes of gaining some level of understanding behind the seemingly absolute response you originally offered.


The OP asked about events, not hooking windows messages.

 

I asked for more specifics because there are ways to handle things like pointer movements in the drawing editor without having to handle low-level input events.

 

 

 

 

Message 12 of 15
Anonymous
in reply to: DiningPhilosopher

Anonymous
Not applicable

I actually ended up creating a new modeless window that takes control when focused. Basically got to where layer walk is. Gee, what a test.

 

What prevents me from marking this topic as solved is whether the message filter could be applied application wide. That is, whether a message filter can be added on the plugin initialization.

0 Likes

I actually ended up creating a new modeless window that takes control when focused. Basically got to where layer walk is. Gee, what a test.

 

What prevents me from marking this topic as solved is whether the message filter could be applied application wide. That is, whether a message filter can be added on the plugin initialization.

Message 13 of 15
DiningPhilosopher
in reply to: Anonymous

DiningPhilosopher
Collaborator
Collaborator

@Anonymous wrote:

I actually ended up creating a new modeless window that takes control when focused. Basically got to where layer walk is. Gee, what a test.

 

What prevents me from marking this topic as solved is whether the message filter could be applied application wide. That is, whether a message filter can be added on the plugin initialization.


Message filters can be added at any time during a plugin's life.


@Anonymous wrote:

I actually ended up creating a new modeless window that takes control when focused. Basically got to where layer walk is. Gee, what a test.

 

What prevents me from marking this topic as solved is whether the message filter could be applied application wide. That is, whether a message filter can be added on the plugin initialization.


Message filters can be added at any time during a plugin's life.

Message 14 of 15
Anonymous
in reply to: DiningPhilosopher

Anonymous
Not applicable
Accepted solution

Alright. Although I didn't actually apply that in the end, it is the answer to the initial question:


@BlackBoxCAD wrote:

Forgive my curiosity, but while this is no Ctrl+MouseWheel Event handler, what's wrong with DoEvents(), and acedUsrBrk() to catch user input?

 

Quote:

 

"It's fairly common for developers to want to check for user input from time to time during long operations, especially to see whether the user wants to cancel the current activity. In VB you'd use DoEvents() to enable messages to be processed by the application's message loop and in ObjectARX you'd use acedUsrBrk().

So how to do this in .NET?

 

The answer is to use a message filter. This allows us to check on user-input events... we still call DoEvents, as with previous versions of VB, which allows user input events (such as keystrokes) to flow into our message filter function. We can then detect the events we care about, and filter them out, if necessary."

 

http://through-the-interface.typepad.com/through_the_interface/2007/02/index.html



@DiningPhilosopher wrote:

Message filters can be added at any time during a plugin's life.


With a really strong notions of dangers of such approach:


@DiningPhilosopher wrote:

If you use an IMessageFilter and check if certain keys are pressed, and what is currently happening in AutoCAD, you might be able to do what you need.  You can use System.Windows.Forms.ModifierKeys to check if CTR/SHIFT/ALT are pressed when you see a mouse wheel message, but you'd have to find a combination of keys that aren't used by AutoCAD with mouse wheel activity, if you don't want to step on that.


Thanks everyone.

0 Likes

Alright. Although I didn't actually apply that in the end, it is the answer to the initial question:


@BlackBoxCAD wrote:

Forgive my curiosity, but while this is no Ctrl+MouseWheel Event handler, what's wrong with DoEvents(), and acedUsrBrk() to catch user input?

 

Quote:

 

"It's fairly common for developers to want to check for user input from time to time during long operations, especially to see whether the user wants to cancel the current activity. In VB you'd use DoEvents() to enable messages to be processed by the application's message loop and in ObjectARX you'd use acedUsrBrk().

So how to do this in .NET?

 

The answer is to use a message filter. This allows us to check on user-input events... we still call DoEvents, as with previous versions of VB, which allows user input events (such as keystrokes) to flow into our message filter function. We can then detect the events we care about, and filter them out, if necessary."

 

http://through-the-interface.typepad.com/through_the_interface/2007/02/index.html



@DiningPhilosopher wrote:

Message filters can be added at any time during a plugin's life.


With a really strong notions of dangers of such approach:


@DiningPhilosopher wrote:

If you use an IMessageFilter and check if certain keys are pressed, and what is currently happening in AutoCAD, you might be able to do what you need.  You can use System.Windows.Forms.ModifierKeys to check if CTR/SHIFT/ALT are pressed when you see a mouse wheel message, but you'd have to find a combination of keys that aren't used by AutoCAD with mouse wheel activity, if you don't want to step on that.


Thanks everyone.

Message 15 of 15
Anonymous
in reply to: DiningPhilosopher

Anonymous
Not applicable

The code below is working fine for selecting 3 parts 

 

 

' To suppress a part

Public Sub AssemblyCount()

    ' Set reference to active document.

    ' This assumes the active document is an assembly

    Dim oDoc As Inventor.AssemblyDocument

    Set oDoc = ThisApplication.ActiveDocument

    Dim selset As SelectSet

    Dim compOcc As ComponentOccurrence

    oDoc.SelectSet.Clear

    MsgBox ("Select a Part or a Subassembly")

    Do Until oDoc.SelectSet.Count = 3

        DoEvents    ' Yield to other processes.

    Loop

    Set selset = oDoc.SelectSet

    Dim obj As Object

    For Each obj In selset

        If TypeOf obj Is ComponentOccurrence Then

               Set compOcc = obj

               compOcc.Suppress

        End If

    Next

    oDoc.SelectSet.Clear

End Sub

 

 

But i want to select multiple parts until i hit enter button after hitting enter button all the selected parts must be suppressed

 

I tried the Application.onkey 

 

Public enterWasPressed As Boolean

' To suppress a part

Public Sub AssemblyCount()

    ' Set reference to active document.

    ' This assumes the active document is an assembly

    Dim oDoc As Inventor.AssemblyDocument

    Set oDoc = ThisApplication.ActiveDocument

    Dim selset As SelectSet

    Dim compOcc As ComponentOccurrence

   

    oDoc.SelectSet.Clear

    MsgBox ("Select a Part or a Subassembly")

    enterWasPressed = False

    Do Until enterWasPressed = True

        DoEvents    ' Yield to other processes.

        Application.OnKey "{ENTER}", "recordEnterKeypress"

    Loop

 

 

    Set selset = oDoc.SelectSet

    Dim obj As Object

    For Each obj In selset

        If TypeOf obj Is ComponentOccurrence Then

               Set compOcc = obj

               compOcc.Suppress

        End If

    Next

    oDoc.SelectSet.Clear

End Sub

 

   

 

Sub recordEnterKeypress()

    enterWasPressed = True

End Sub

 

 

but i am getting runtime error '424' object required can anyone please help me to fix my issue

 

 

When i am searching in google i saw your post to use IMessageFilter to handle keyboard and mouse event.Can you please help me to fix my issue by writing the apt code for it. Since i am new to autodesk and i am not familiar with IMessageFilter i am asking you for the code.

 

 

First i need to press a command button in the user form and then go to inventor to select the part or assemblies so that it can be suppressed on press of enter key

 

0 Likes

The code below is working fine for selecting 3 parts 

 

 

' To suppress a part

Public Sub AssemblyCount()

    ' Set reference to active document.

    ' This assumes the active document is an assembly

    Dim oDoc As Inventor.AssemblyDocument

    Set oDoc = ThisApplication.ActiveDocument

    Dim selset As SelectSet

    Dim compOcc As ComponentOccurrence

    oDoc.SelectSet.Clear

    MsgBox ("Select a Part or a Subassembly")

    Do Until oDoc.SelectSet.Count = 3

        DoEvents    ' Yield to other processes.

    Loop

    Set selset = oDoc.SelectSet

    Dim obj As Object

    For Each obj In selset

        If TypeOf obj Is ComponentOccurrence Then

               Set compOcc = obj

               compOcc.Suppress

        End If

    Next

    oDoc.SelectSet.Clear

End Sub

 

 

But i want to select multiple parts until i hit enter button after hitting enter button all the selected parts must be suppressed

 

I tried the Application.onkey 

 

Public enterWasPressed As Boolean

' To suppress a part

Public Sub AssemblyCount()

    ' Set reference to active document.

    ' This assumes the active document is an assembly

    Dim oDoc As Inventor.AssemblyDocument

    Set oDoc = ThisApplication.ActiveDocument

    Dim selset As SelectSet

    Dim compOcc As ComponentOccurrence

   

    oDoc.SelectSet.Clear

    MsgBox ("Select a Part or a Subassembly")

    enterWasPressed = False

    Do Until enterWasPressed = True

        DoEvents    ' Yield to other processes.

        Application.OnKey "{ENTER}", "recordEnterKeypress"

    Loop

 

 

    Set selset = oDoc.SelectSet

    Dim obj As Object

    For Each obj In selset

        If TypeOf obj Is ComponentOccurrence Then

               Set compOcc = obj

               compOcc.Suppress

        End If

    Next

    oDoc.SelectSet.Clear

End Sub

 

   

 

Sub recordEnterKeypress()

    enterWasPressed = True

End Sub

 

 

but i am getting runtime error '424' object required can anyone please help me to fix my issue

 

 

When i am searching in google i saw your post to use IMessageFilter to handle keyboard and mouse event.Can you please help me to fix my issue by writing the apt code for it. Since i am new to autodesk and i am not familiar with IMessageFilter i am asking you for the code.

 

 

First i need to press a command button in the user form and then go to inventor to select the part or assemblies so that it can be suppressed on press of enter key

 

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report