Page 1 of 1

[SOLVED] Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 12:07 am
by Oso
I wondered if anyone knows if it's possible to prevent CTRL/C from closing an OpenConsole() window?

Even if there isn't any keyboard input detection in the code, the window still closes as soon as the user presses CTRL/C.

There are also two keys I'd like to be able to detect if possible, because I'm trying to emulate similar functions from another application. These are :

CTRL/A (I can't detect it because Windows uses this key function to select all characters on the screen)
CTRL/F (I can't detect it because Windows invokes a Find function)

Maybe they are windows shell functions, over which PB has no control, but thought I'd ask if anyone knows some magic. In case it's relevant, I detect input using the below. For those three control functions I've mentioned, they never get detected at the Inkey() and RawKey().

Code: Select all

inpkey.s = Inkey()                                                    ; Returns ASCII input
keycode.b = RawKey()                                                  ; Returns keyboard code if input is not a conventional ASCII input

Re: Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 4:30 am
by jacdelad
Afaik, for Windows, this is not possible on newer Windows versions. If I remember right, there was a fix for older system floating around somewhere here, because this was asked several times.

Re: Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 8:55 am
by Oso
jacdelad wrote: Tue Oct 24, 2023 4:30 am Afaik, for Windows, this is not possible on newer Windows versions.
Thanks, I suppose we have to live with it. I considered re-writing the app. as a GUI but there's nothing quite like the console for its speed of output, its simplicity and convenience.

Re: Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 10:46 am
by BarryG
Can you set a hotkey in PureBasic to Ctrl+C and make it do nothing?

Re: Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 10:53 am
by fryquez
Use SetConsoleCtrlHandler_() to handle control key signals.

Re: Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 11:13 am
by jacdelad
If I remember right it calls the function and closes anyway.
fryquez wrote: Tue Oct 24, 2023 10:53 am Use SetConsoleCtrlHandler_() to handle control key signals.

Re: Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 12:56 pm
by Oso
I've done it, adapting an old example from Fred, replacing #CTRL_CLOSE_EVENT with #CTRL_C_EVENT :

Code: Select all

Procedure console_handler(my_event.l)
  If my_event = #CTRL_C_EVENT
    PrintN("CTRL/C pressed")
    ProcedureReturn #True
  EndIf
  
  If my_event = #CTRL_BREAK_EVENT
    PrintN("CTRL/BREAK pressed")
    ProcedureReturn #True
  EndIf
  
  If my_event = #CTRL_CLOSE_EVENT
    PrintN("CLOSE clicked")
    Delay(2000)
    ProcedureReturn #True
  EndIf
  
  ProcedureReturn #False
EndProcedure

OpenConsole()
SetConsoleCtrlHandler_(@console_handler(), 1)

PrintN("Try to close the console with CTRL/C or CTRL/BREAK")
Repeat
  Delay(20)
ForEver

Re: Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 1:58 pm
by Oso
fryquez wrote: Tue Oct 24, 2023 10:53 am Use SetConsoleCtrlHandler_() to handle control key signals.
jacdelad wrote: Tue Oct 24, 2023 11:13 am If I remember right it calls the function and closes anyway.
Yes, that's what happens when the window is closed, jacdelad. It calls the function then closes. This is okay in my case, as I don't mind if the window is closed in that way, but for CTRL/C it was too easy to accidentally close the app.

Re: [SOLVED] Inhibit CTRL/C in the console

Posted: Tue Oct 24, 2023 4:31 pm
by Mijikai
Mby:

Code: Select all

SetConsoleCtrlHandler_(#Null,#True)