DavidKarner Posted February 18, 2009 Posted February 18, 2009 I have a console mode script which I need trap CTRL-C so that my program can perform some cleanup before exiting. The OnAutoItExist() function does not trigger on CTRL-C or CTRL-Break events. Per some comments from Valik, he indicated that I could work around this issue by using DLLCall and changing the way the console processes CTRL-C. However, I am struggling with implementing this technique and was hopping someone could offer some assistance. I am trying to use the Kernel32.dll call "GetConsoleMode" to determine to current mode so I can modify it to disable the ENABLE_PROCESSED_INPUT option. In the script below I am trying both GetConsoleWindow and GetStdHandle to return a handle and query GetConsoleMode but in both cases the GetConsoleMode returns a failure and reports that it is an invalid handle. Does anyone know how to determine the correct ConsoleHandle that is required to pass into the GetConsoleWindow function? #Region;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Change2CUI=y #EndRegion;**** Directives created by AutoIt3Wrapper_GUI **** Const $STD_INPUT_HANDLE = -10 Local $DllValue $DllValue = DllCall( "Kernel32.dll","hwnd","GetConsoleWindow") ConsoleWrite("Handle: " & $DllValue[0] & " ConsoleMode: " & _GetConsoleMode( $DllValue[0] ) & @CRLF ) $DllValue = DllCall( "Kernel32.dll","hwnd","GetStdHandle","dword",$STD_INPUT_HANDLE) ConsoleWrite("Handle: " & $DllValue[0] & " ConsoleMode: " & _GetConsoleMode( $DllValue[0] ) & @CRLF ) Func _GetConsoleMode( $Handle ) ; Create Key Structure Local $DllStructure = DllStructCreate( "Dword iConsoleMode" ) DllCall( "Kernel32.dll","int","GetConsoleMode","hwnd",$Handle,"ptr",DllStructGetPtr( $DllStructure)) Return DllStructGetData( $DllStructure, "iConsoleMode" ) EndFunc
DavidKarner Posted February 18, 2009 Author Posted February 18, 2009 I was able to do the following which traps CTRL-C (but not CTRL-Break). Still hoping to find a way to get the GetConsoleMode/SetConsoleMode calls working. #Region;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Change2CUI=y #EndRegion;**** Directives created by AutoIt3Wrapper_GUI **** Local $DllValue, $Handle $DllValue = DllCall( "Kernel32.dll","hwnd","GetConsoleWindow") $Handle = $DllValue[0] HotKeySet( "^c","ControlC" ) While True Sleep(10000) Wend Func ControlC() If WinActive( $Handle, "" ) Then ConsoleWrite( "You hit Control-C" & @CRLF ) Else HotKeySet( "^c" ) Send( "^c" ) HotKeySet( "^c","ControlC" ) EndIf EndFunc
exodius Posted February 18, 2009 Posted February 18, 2009 I was able to do the following which traps CTRL-C (but not CTRL-Break). Still hoping to find a way to get the GetConsoleMode/SetConsoleMode calls working. #Region;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Change2CUI=y #EndRegion;**** Directives created by AutoIt3Wrapper_GUI **** Local $DllValue, $Handle $DllValue = DllCall( "Kernel32.dll","hwnd","GetConsoleWindow") $Handle = $DllValue[0] HotKeySet( "^c","ControlC" ) While True Sleep(10000) Wend Func ControlC() If WinActive( $Handle, "" ) Then ConsoleWrite( "You hit Control-C" & @CRLF ) Else HotKeySet( "^c" ) Send( "^c" ) HotKeySet( "^c","ControlC" ) EndIf EndFunc Does this not do it? Per the page on keys for Send in the HelpFile? {BREAK} for Ctrl+Break processing HotKeySet ("^{BREAK}", "function")
DavidKarner Posted February 18, 2009 Author Posted February 18, 2009 Yes. I was able to add another HotKetSet HotKeySet( "^{BREAK}","ControlBreak" ) And I changed the code as well to send the event. Func ControlBreak() If WinActive( $Handle, "" ) Then ConsoleWrite( "You hit Control-Break" & @CRLF ) DllCall( "Kernel32.dll","int","GenerateConsoleCtrlEvent","dword",1,"dword",0) Else HotKeySet( "^{BREAK}" ) Send( "^{BREAK}" ) HotKeySet( "^{BREAK}","ControlBreak" ) EndIf EndFunc
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now