[SOLVED] Inhibit CTRL/C in the console

Windows specific forum
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

[SOLVED] Inhibit CTRL/C in the console

Post 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
Last edited by Oso on Tue Oct 24, 2023 3:37 pm, edited 2 times in total.
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Inhibit CTRL/C in the console

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Inhibit CTRL/C in the console

Post 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.
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Inhibit CTRL/C in the console

Post by BarryG »

Can you set a hotkey in PureBasic to Ctrl+C and make it do nothing?
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: Inhibit CTRL/C in the console

Post by fryquez »

Use SetConsoleCtrlHandler_() to handle control key signals.
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Inhibit CTRL/C in the console

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Inhibit CTRL/C in the console

Post 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
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Inhibit CTRL/C in the console

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

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

Post by Mijikai »

Mby:

Code: Select all

SetConsoleCtrlHandler_(#Null,#True)
Post Reply