Skip to content

After closing app using this library, Ctrl+C no longer works in terminal #44

@gp-rigado

Description

@gp-rigado

Whenever I exit my app, I can't Ctrl+C or use certain other commands in my terminal. I'm on MacOS and using iterm2.

Here is my code - I usually exit the app cleanly by hitting q

// Start keyboard listener
if err := keyboard.Open(); err != nil {
	fmt.Printf("Error opening keyboard: %v\n", err)
	os.Exit(1)
}
defer keyboard.Close()

// Start a goroutine to watch for the 'q' key
go func() {
	for {
		char, key, err := keyboard.GetKey()
		if err != nil {
			if strings.Contains(err.Error(), "cancel") {
				fmt.Println("exiting")
				keyboard.Close()
				return
			}
			keyboard.Close()
			fmt.Printf("Error getting key: %v\n", err)
			return
		}
		if char == 'q' || key == keyboard.KeyEsc {
			fmt.Println("\nReceived quit signal")
			cancel()
			return
		}
		if key == keyboard.KeyCtrlC {
			fmt.Println("\nReceived interrupt signal")
			os.Exit(0) // Exit immediately
		}
	}
}()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions