Skip to content

Display and control your Android device

Properly handle Ctrl+C on Windows

Romain Vimont28abd98f7fef
diff --git a/README.md b/README.md
index 09cb27b8..0d0c4c5e 100644
--- a/README.md
+++ b/README.md
@@ -210,7 +210,6 @@ To disable mirroring while recording:
scrcpy --no-display --record file.mp4
scrcpy -Nr file.mkv
# interrupt recording with Ctrl+C
-# Ctrl+C does not terminate properly on Windows, so disconnect the device
```
"Skipped frames" are recorded, even if they are not displayed in real time (for
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 096b0794..2a4702e6 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -7,6 +7,10 @@
#include <sys/time.h>
#include <SDL2/SDL.h>
+#ifdef _WIN32
+# include <windows.h>
+#endif
+
#include "config.h"
#include "command.h"
#include "common.h"
@@ -45,6 +49,18 @@ static struct input_manager input_manager = {
.prefer_text = false, // initialized later
};
+#ifdef _WIN32
+BOOL windows_ctrl_handler(DWORD ctrl_type) {
+ if (ctrl_type == CTRL_C_EVENT) {
+ SDL_Event event;
+ event.type = SDL_QUIT;
+ SDL_PushEvent(&event);
+ return TRUE;
+ }
+ return FALSE;
+}
+#endif // _WIN32
+
// init SDL and set appropriate hints
static bool
sdl_init_and_configure(bool display, const char *render_driver) {
@@ -56,6 +72,14 @@ sdl_init_and_configure(bool display, const char *render_driver) {
atexit(SDL_Quit);
+#ifdef _WIN32
+ // Clean up properly on Ctrl+C on Windows
+ bool ok = SetConsoleCtrlHandler(windows_ctrl_handler, TRUE);
+ if (!ok) {
+ LOGW("Could not set Ctrl+C handler");
+ }
+#endif // _WIN32
+
if (!display) {
return true;
}