.NET

Reply
Active Contributor
WWFreeman
Posts: 27
Registered: ‎09-02-2012
Message 1 of 14 (1,049 Views)
Accepted Solution

Catching Keyboard and Mouse Events

1049 Views, 13 Replies
03-07-2013 12:15 PM

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!

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 14 (1,043 Views)

Re: Catching Keyboard and Mouse Events

03-07-2013 12:36 PM in reply to: WWFreeman

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.

Active Contributor
WWFreeman
Posts: 27
Registered: ‎09-02-2012
Message 3 of 14 (1,034 Views)

Re: Catching Keyboard and Mouse Events

03-07-2013 01:00 PM in reply to: DiningPhilosopher

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.

Distinguished Mentor
BlackBox_
Posts: 744
Registered: ‎02-25-2013
Message 4 of 14 (1,012 Views)

Re: Catching Keyboard and Mouse Events

03-07-2013 02:08 PM in reply to: DiningPhilosopher

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



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 5 of 14 (995 Views)

Re: Catching Keyboard and Mouse Events

03-07-2013 09:27 PM in reply to: WWFreeman

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.

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 6 of 14 (994 Views)

Re: Catching Keyboard and Mouse Events

03-07-2013 09:32 PM in reply to: BlackBox_

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..

Active Contributor
WWFreeman
Posts: 27
Registered: ‎09-02-2012
Message 7 of 14 (990 Views)

Re: Catching Keyboard and Mouse Events

03-07-2013 09:57 PM in reply to: DiningPhilosopher

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.

Distinguished Mentor
BlackBox_
Posts: 744
Registered: ‎02-25-2013
Message 8 of 14 (983 Views)

Re: Catching Keyboard and Mouse Events

03-07-2013 10:09 PM in reply to: DiningPhilosopher

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



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

Distinguished Mentor
BlackBox_
Posts: 744
Registered: ‎02-25-2013
Message 9 of 14 (962 Views)

Re: Catching Keyboard and Mouse Events

03-08-2013 08:37 AM in reply to: BlackBox_

"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



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 10 of 14 (950 Views)

Re: Catching Keyboard and Mouse Events

03-08-2013 03:42 PM in reply to: WWFreeman

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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community