sapi_windows_set_ctrl_handler()
(PHP 7 >= 7.4.0)
Set or remove a CTRL event handler
说明
sapi_windows_set_ctrl_handler(callable $callable[,bool $add=TRUE]): boolSets or removes aCTRLevent handler, which allows Windows CLI processes to intercept or ignoreCTRL+CandCTRL+BREAKevents. Note that in multithreaded environments, this is only possible when called from the main thread.
参数
- $callable
A callback function to set or remove. If set, this function will be called whenever aCTRL+CorCTRL+BREAKevent occurs. The function is supposed to have the following signature:
handler(int $event): void
- $event
- TheCTRLevent which has been received;either
PHP_WINDOWS_EVENT_CTRL_CorPHP_WINDOWS_EVENT_CTRL_BREAK.
NULL$callablecauses the process to ignoreCTRL+Cevents, but notCTRL+BREAKevents.- $add
If
TRUE, the handler is set.IfFALSE, the handler is removed.
返回值
成功时返回TRUE,或者在失败时返回FALSE。
范例
Basicsapi_windows_set_ctrl_handler()Usage
This example shows how to interceptCTRLevents.
<?php
function ctrl_handler(int $event)
{
switch ($event) {
case PHP_WINDOWS_EVENT_CTRL_C:
echo "You have pressed CTRL+C\n";
break;
case PHP_WINDOWS_EVENT_CTRL_BREAK:
echo "You have pressed CTRL+BREAK\n";
break;
}
}
sapi_windows_set_ctrl_handler('ctrl_handler');
while (true); // infinite loop, so the handler can be triggered
?>
参见
sapi_windows_generate_ctrl_event()Send a CTRL event to another process
