[Closed] [MXS and DotNet] KeyDown throws error
Hi everyone,
I’m experiencing a weird problem. Here is my test simplified code for you to understand the problem
fn keyDown s e =
(
format "key down: %
" e
)
dnForm = dotNetObject "MaxCustomControls.MaxForm"
dnForm.showModeless()
dotNet.setLifeTimeControl dnForm #dotNet
dotNet.addEventHandler dnForm "KeyDown" keyDown
I wanna catch the KeyDown event but i work for every key except the Escape one. Every time i press it, it throws an error :
*********** Exception Text ***********
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at MXS_dotNet.DotNetObjectWrapper.RunEventHandlers(DotNetObjectWrapper* , Value* target, Value* eventName, Array* handlers, Object[] delegateArgsArray)
at MXS_dotNet.DotNetObjectWrapper.ProcessEvent(DotNetObjectWrapper* , String eventName, Object delegateArgs)
at MXS_dotNet.DotNetObjectManaged.delegate_proxy_type.ProcessEvent(String eventName, Object delegateArgs)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
If i use only the KeyUp event instead of KeyDown everything works fine but it’s not the behaviour i want for my script. Maybe i’m doing something wrong.
Thanks.
Tony.
EDIT : was just trying with :
fn keyDown s e =
(
format "key down: %
" e
)
fn keyUp s e =
(
format "key up: %
" e
)
dnForm = dotNetObject "System.Windows.Forms.Form"
dnForm.show()
dotNet.setLifeTimeControl dnForm #dotNet
dotNet.addEventHandler dnForm "KeyDown" keyDown
dotNet.addEventHandler dnForm "KeyUp" keyUP
And it worked. I think this is the Maxform that can’t handle correctly keydown event
EDIT 2 :
New problem the DotNet Form doesn’t handle F1-F12 key , maxForm does…
I tried without that line and still crashing.
I have two solutions :
- Using a maxform but i need to figure out how to handle Escape pressing without crashing
- Using a standard dotnet form but i need to figure out how to handle F1-F12 keys (actually when i press F9 for example it launch the render)
Do you have ideas ?
_maxform =
(
try(_maxform.Close()) catch()
local form = dotnetobject "MaxCustomControls.MaxForm"
form.Text = "MAX Form"
form.StartPosition = form.StartPosition.Manual
form.Bounds = dotnetobject "System.Drawing.Rectangle" 600 100 200 80
fn onKeyDown s e =
(
format "key down: %
" (e.KeyCode.ToString())
if e.KeyCode == e.KeyCode.Escape do s.Close()
)
dotnet.addeventhandler form "KeyDown" onKeyDown
form.showModeless()
form
)
_dotform =
(
try(_dotform.Close()) catch()
local form = dotnetobject "Form"
form.Text = ".NET Form"
form.ShowInTaskbar = off
form.StartPosition = form.StartPosition.Manual
form.Bounds = dotnetobject "System.Drawing.Rectangle" 600 200 200 80
fn onGotFocus s e = (enableAccelerators = off)
fn onLostFocus s e = (enableAccelerators = on)
dotnet.addeventhandler form "GotFocus" onGotFocus
dotnet.addeventhandler form "LostFocus" onLostFocus
fn onKeyDown s e =
(
format "key down: %
" (e.KeyCode.ToString())
if e.KeyCode == e.KeyCode.Escape do s.Close()
)
dotnet.addeventhandler form "KeyDown" onKeyDown
MaxHandleWrapper = dotnetobject "MaxCustomControls.Win32HandleWrapper" (dotnetobject "System.IntPtr" (windows.getmaxhwnd()))
form.Show MaxHandleWrapper
form
)
there is no any problem for me for versions 2010-2012. do you use 64-bits version?
code is 100% correct and can’t crash by itself. it might be something in your system that causes the crash.
btw… what does the system say after the crash?
*********** Exception Text ***********
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at MXS_dotNet.DotNetObjectWrapper.RunEventHandlers(Do tNetObjectWrapper* , Value* target, Value* eventName, Array* handlers, Object[] delegateArgsArray)
at MXS_dotNet.DotNetObjectWrapper.ProcessEvent(DotNet ObjectWrapper* , String eventName, Object delegateArgs)
at MXS_dotNet.DotNetObjectManaged.delegate_proxy_type .ProcessEvent(String eventName, Object delegateArgs)
at System.Windows.Forms.Control.ProcessKeyEventArgs(M essage& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
That’s the crash after that i need to close the dotnet form. Sometime it make max crash too. I’m using 64bits version of max
If you run my code on my first post does it crash on your computer?