mode.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //go:build windows
  2. // +build windows
  3. package coninput
  4. import (
  5. "strings"
  6. "golang.org/x/sys/windows"
  7. )
  8. // AddInputModes returns the given mode with one or more additional modes enabled.
  9. func AddInputModes(mode uint32, enableModes ...uint32) uint32 {
  10. for _, enableMode := range enableModes {
  11. mode |= enableMode
  12. }
  13. return mode
  14. }
  15. // RemoveInputModes returns the given mode with one or more additional modes disabled.
  16. func RemoveInputModes(mode uint32, disableModes ...uint32) uint32 {
  17. for _, disableMode := range disableModes {
  18. mode &^= disableMode
  19. }
  20. return mode
  21. }
  22. // ToggleInputModes returns the given mode with one or more additional modes toggeled.
  23. func ToggleInputModes(mode uint32, toggleModes ...uint32) uint32 {
  24. for _, toggeMode := range toggleModes {
  25. mode ^= toggeMode
  26. }
  27. return mode
  28. }
  29. var inputModes = []struct {
  30. mode uint32
  31. name string
  32. }{
  33. {mode: windows.ENABLE_ECHO_INPUT, name: "ENABLE_ECHO_INPUT"},
  34. {mode: windows.ENABLE_INSERT_MODE, name: "ENABLE_INSERT_MODE"},
  35. {mode: windows.ENABLE_LINE_INPUT, name: "ENABLE_LINE_INPUT"},
  36. {mode: windows.ENABLE_MOUSE_INPUT, name: "ENABLE_MOUSE_INPUT"},
  37. {mode: windows.ENABLE_PROCESSED_INPUT, name: "ENABLE_PROCESSED_INPUT"},
  38. {mode: windows.ENABLE_QUICK_EDIT_MODE, name: "ENABLE_QUICK_EDIT_MODE"},
  39. {mode: windows.ENABLE_WINDOW_INPUT, name: "ENABLE_WINDOW_INPUT"},
  40. {mode: windows.ENABLE_VIRTUAL_TERMINAL_INPUT, name: "ENABLE_VIRTUAL_TERMINAL_INPUT"},
  41. }
  42. // ListInputMode returnes the isolated enabled input modes as a list.
  43. func ListInputModes(mode uint32) []uint32 {
  44. modes := []uint32{}
  45. for _, inputMode := range inputModes {
  46. if mode&inputMode.mode > 0 {
  47. modes = append(modes, inputMode.mode)
  48. }
  49. }
  50. return modes
  51. }
  52. // ListInputMode returnes the isolated enabled input mode names as a list.
  53. func ListInputModeNames(mode uint32) []string {
  54. modes := []string{}
  55. for _, inputMode := range inputModes {
  56. if mode&inputMode.mode > 0 {
  57. modes = append(modes, inputMode.name)
  58. }
  59. }
  60. return modes
  61. }
  62. // DescribeInputMode returns a string containing the names of each enabled input mode.
  63. func DescribeInputMode(mode uint32) string {
  64. return strings.Join(ListInputModeNames(mode), "|")
  65. }