Sign in to follow this  
Followers 0
DavidKarner

Trapping CTRL-C in console mode scripts

4 posts in this topic

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

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

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")

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0