CGTalk > Software > Autodesk 3ds max > 3dsMax SDK and MaxScript
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 06-10-2013, 02:10 PM   #1
grabjacket
Connecting 3D
 
grabjacket's Avatar
portfolio
Klaas Nienhuis
Schiedam, Netherlands
 
Join Date: Sep 2007
Posts: 246
catching mouseclicks

Hi,

I'm working on a script to make screengrabs of anything in 3dsMax: viewports, dialogs, scripts. Just still images. I'm using this code by DenisT to catch mouseclicks anywhere on the screen. This works pretty good but there's a problem. When I press the ESC key a few times I get an error and have to quit 3dsMax to get out of it.
Is there a way to block the esc key while this script is running? I've tried to catch the esc-key but this works only sometimes, while other times the same error pops up.

Klaas
 
Old 06-10-2013, 02:21 PM   #2
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,637
Send a message via ICQ to denisT
i can reproduce the crash with only my code run... probably something else causes that
 
Old 06-10-2013, 02:23 PM   #3
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,637
Send a message via ICQ to denisT
Quote:
Originally Posted by grabjacket
while other times the same error pops up.

what does this error say?
 
Old 06-10-2013, 02:28 PM   #4
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,637
Send a message via ICQ to denisT
Quote:
Originally Posted by grabjacket
Is there a way to block the esc key while this script is running?

the only way is to use Keyboard Hook (WH_KEYBOARD_LL) ... with Mouse Hook will be much easier and cleaner to catch mouse clicks.

but i don't want to show on this forum how to write a hook. because i cannot be sure that all people will use it intelligently
 
Old 06-10-2013, 03:17 PM   #5
grabjacket
Connecting 3D
 
grabjacket's Avatar
portfolio
Klaas Nienhuis
Schiedam, Netherlands
 
Join Date: Sep 2007
Posts: 246
Hi Denis,

this is the error, nothing is printed to the listener however.



I've got it in max 2012 and 2013. Try to tap the esc key as fast as you can 5 to 10 times. That triggers the error for me each time.
 
Old 06-10-2013, 04:48 PM   #6
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,637
Send a message via ICQ to denisT
Quote:
Originally Posted by grabjacket
I've got it in max 2012 and 2013. Try to tap the esc key as fast as you can 5 to 10 times. That triggers the error for me each time.

it doesn't crash for me...
if you do some operations in mouseTimerTick function set timer.enabled to OFF before the an operation, and to ON after. it might help.

Last edited by denisT : 06-10-2013 at 05:00 PM.
 
Old 06-10-2013, 06:39 PM   #7
grabjacket
Connecting 3D
 
grabjacket's Avatar
portfolio
Klaas Nienhuis
Schiedam, Netherlands
 
Join Date: Sep 2007
Posts: 246
Issue in timer

The issue seems to be in the timer, like you suggest. I've replaced this timer with another timer which seems to resolve the problem. If I press the ESC key a few times quickly the timer stops, but doesn't give me an error. For me this is OK, but it's still odd. Maybe other people can confirm this as well.
Code:
try(mouse_timer.Dispose()) catch() mouse_timer = dotnetobject "System.Timers.Timer" mouse_timer.interval = 100 fn mouseTimerTick s e = ( format "pos %\n" mouse.screenPos ) dotnet.addEventHandler mouse_timer "Elapsed" mouseTimerTick mouse_timer.Start()
 
Old 06-11-2013, 11:49 AM   #8
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,637
Send a message via ICQ to denisT
i've played with System.Windows.Forms.Timer. now i'm in max 2012, and seams like the timer works differently than it worked in max 2010. it looks like it takes a focus on every tick, and returns after the event callback. but if the focus was changed during the event callback, it doesn't return it right. definitely i don't remember this behavior on previous versions... and i need to double-check it. but the fact is - some of my scripts which worked well in max 2010 stopped working correctly in max 2012
 
Old 06-12-2013, 12:16 AM   #9
Kameleon
Expert
 
Kameleon's Avatar
portfolio
Artur Leao
Co-Founder / Project Manager
You can do it! VFX
Porto, Portugal
 
Join Date: Sep 2004
Posts: 1,022
Two things that might sound stupid:

escapeEnabled ? Also dont forget to dotNet.setLifetimeControl on your .net timer
__________________
Artur Leao | Co-Founder / Project Manager
You can do it! VFX
Porto/Lisbon - Portugal
http://www.ycdivfx.com
 
Old 06-12-2013, 06:37 AM   #10
grabjacket
Connecting 3D
 
grabjacket's Avatar
portfolio
Klaas Nienhuis
Schiedam, Netherlands
 
Join Date: Sep 2007
Posts: 246
Well, it's my first time using such a timer, so nothing sounds stupid to me. Could you show in my example how to do it?
 
Old 06-12-2013, 10:54 AM   #11
Kameleon
Expert
 
Kameleon's Avatar
portfolio
Artur Leao
Co-Founder / Project Manager
You can do it! VFX
Porto, Portugal
 
Join Date: Sep 2004
Posts: 1,022
Just add this:

dotNet.setLifetimeControl mouse_timer #dotnet

and this

escapeEnable = false -- turn off MAXScript escape processing

Read more about here:

http://docs.autodesk.com/3DSMAX/15/...mber=d30e717588

http://docs.autodesk.com/3DSMAX/15/...umber=d30e31247
__________________
Artur Leao | Co-Founder / Project Manager
You can do it! VFX
Porto/Lisbon - Portugal
http://www.ycdivfx.com
 
Old 06-18-2013, 08:20 AM   #12
grabjacket
Connecting 3D
 
grabjacket's Avatar
portfolio
Klaas Nienhuis
Schiedam, Netherlands
 
Join Date: Sep 2007
Posts: 246
The result

Thanks for the help guys,

I've written yet another screencapture script to capture stills which uses this timer. This one however is not limited to viewports. It can capture any window or control inside or outside 3dsMax.

Check out the blog and download it here

Last edited by grabjacket : 06-18-2013 at 08:33 AM. Reason: Changed download link
 
Old 06-18-2013, 01:26 PM   #13
Kameleon
Expert
 
Kameleon's Avatar
portfolio
Artur Leao
Co-Founder / Project Manager
You can do it! VFX
Porto, Portugal
 
Join Date: Sep 2004
Posts: 1,022
Great stuff and I really like your code, clean and easy to read! Congrats and thanks.
__________________
Artur Leao | Co-Founder / Project Manager
You can do it! VFX
Porto/Lisbon - Portugal
http://www.ycdivfx.com
 
Old 06-18-2013, 07:36 PM   #14
grabjacket
Connecting 3D
 
grabjacket's Avatar
portfolio
Klaas Nienhuis
Schiedam, Netherlands
 
Join Date: Sep 2007
Posts: 246
Thanks Artur, that's appreciated!
I've left one feature out however which I didn't get to work. Maybe you have some ideas.
I want to preview the window the mouse is hovering over by outlining it with a red border. This happens a lot in other screencapture programs and it makes the user a lot more aware of what he's actually going to capture. I have some code, but it draws the borders inconsistently and also removes them inconsistently. It gives you a rough idea however of what I'm trying to do.
Here's the script
And here is the assembly I'm using to make it happen
Code:
str="using System;\n" str+="using System.Runtime.InteropServices;\n" str+="using System.Drawing;\n" str+="namespace HotTrack\n" str+="{\n" str+="class DrawOnScreen\n" str+="{\n" --these flags: http://www.blitzbasic.com/Community/posts.php?topic=32334 str+=" private const int RDW_FRAME = 0x0400;\n" str+=" private const int RDW_INVALIDATE = 0x0001;\n" str+=" private const int RDW_UPDATENOW = 0x0100;\n" str+=" private const int RDW_ALLCHILDREN = 0x0080;\n" str+=" public static Graphics G;\n" --move the Graphics object out here. str+=" [DllImport(\"user32.dll\")]\n" str+=" private static extern IntPtr GetWindowDC(IntPtr hWnd);\n" str+=" [DllImport(\"user32.dll\")]\n" str+=" private static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase );\n" str+=" [DllImport(\"user32.dll\")]\n" str+=" private static extern bool UpdateWindow(IntPtr hWnd);\n" str+=" [DllImport(\"user32.dll\")]\n" str+=" private static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, int flags);\n" str+=" [DllImport(\"user32.dll\")]\n" str+=" public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);\n" --resetting the window fails inconsistently. sometimes the red border is removed, sometimes not str+=" public static void ResetWindow(IntPtr hWnd)\n" str+=" {\n" str+=" InvalidateRect(hWnd, IntPtr.Zero, true);\n" str+=" UpdateWindow(hWnd);\n" str+=" RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);\n" str+=" }\n" --http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c581ef21-2833-47e5-90d7-9b8c4f38646c/ str+=" public void DrawWindow(IntPtr hWnd, int width, int height)\n" str+=" {\n" str+=" IntPtr hdc = GetWindowDC(hWnd);\n" str+=" using (var pen = new Pen(Color.Red, 4))\n" str+=" {\n" str+=" using (G = Graphics.FromHdc(hdc))\n" str+=" {\n" str+=" G.DrawRectangle(pen, 2,2, width-4, height-4);\n" str+=" }\n" str+=" }\n" str+=" ReleaseDC(hWnd, hdc);\n" str+=" }\n" str+="}\n" str+="}\n"
 
Old 06-18-2013, 07:36 PM   #15
CGTalk Moderation
Expert
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,478
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 07:11 AM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.