Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win32console_demo.py 5.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import time
  2. import win32con
  3. import win32console
  4. virtual_keys = {}
  5. for k, v in list(win32con.__dict__.items()):
  6. if k.startswith("VK_"):
  7. virtual_keys[v] = k
  8. free_console = True
  9. try:
  10. win32console.AllocConsole()
  11. except win32console.error as exc:
  12. if exc.winerror != 5:
  13. raise
  14. ## only free console if one was created successfully
  15. free_console = False
  16. stdout = win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE)
  17. stdin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
  18. newbuffer = win32console.CreateConsoleScreenBuffer()
  19. newbuffer.SetConsoleActiveScreenBuffer()
  20. newbuffer.SetConsoleTextAttribute(
  21. win32console.FOREGROUND_RED
  22. | win32console.FOREGROUND_INTENSITY
  23. | win32console.BACKGROUND_GREEN
  24. | win32console.BACKGROUND_INTENSITY
  25. )
  26. newbuffer.WriteConsole("This is a new screen buffer\n")
  27. ## test setting screen buffer and window size
  28. ## screen buffer size cannot be smaller than window size
  29. window_size = newbuffer.GetConsoleScreenBufferInfo()["Window"]
  30. coord = win32console.PyCOORDType(X=window_size.Right + 20, Y=window_size.Bottom + 20)
  31. newbuffer.SetConsoleScreenBufferSize(coord)
  32. window_size.Right += 10
  33. window_size.Bottom += 10
  34. newbuffer.SetConsoleWindowInfo(Absolute=True, ConsoleWindow=window_size)
  35. ## write some records to the input queue
  36. x = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
  37. x.Char = "X"
  38. x.KeyDown = True
  39. x.RepeatCount = 1
  40. x.VirtualKeyCode = 0x58
  41. x.ControlKeyState = win32con.SHIFT_PRESSED
  42. z = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
  43. z.Char = "Z"
  44. z.KeyDown = True
  45. z.RepeatCount = 1
  46. z.VirtualKeyCode = 0x5A
  47. z.ControlKeyState = win32con.SHIFT_PRESSED
  48. stdin.WriteConsoleInput([x, z, x])
  49. newbuffer.SetConsoleTextAttribute(
  50. win32console.FOREGROUND_RED
  51. | win32console.FOREGROUND_INTENSITY
  52. | win32console.BACKGROUND_GREEN
  53. | win32console.BACKGROUND_INTENSITY
  54. )
  55. newbuffer.WriteConsole("Press some keys, click some characters with the mouse\n")
  56. newbuffer.SetConsoleTextAttribute(
  57. win32console.FOREGROUND_BLUE
  58. | win32console.FOREGROUND_INTENSITY
  59. | win32console.BACKGROUND_RED
  60. | win32console.BACKGROUND_INTENSITY
  61. )
  62. newbuffer.WriteConsole('Hit "End" key to quit\n')
  63. breakout = False
  64. while not breakout:
  65. input_records = stdin.ReadConsoleInput(10)
  66. for input_record in input_records:
  67. if input_record.EventType == win32console.KEY_EVENT:
  68. if input_record.KeyDown:
  69. if input_record.Char == "\0":
  70. newbuffer.WriteConsole(
  71. virtual_keys.get(
  72. input_record.VirtualKeyCode,
  73. "VirtualKeyCode: %s" % input_record.VirtualKeyCode,
  74. )
  75. )
  76. else:
  77. newbuffer.WriteConsole(input_record.Char)
  78. if input_record.VirtualKeyCode == win32con.VK_END:
  79. breakout = True
  80. break
  81. elif input_record.EventType == win32console.MOUSE_EVENT:
  82. if input_record.EventFlags == 0: ## 0 indicates a button event
  83. if input_record.ButtonState != 0: ## exclude button releases
  84. pos = input_record.MousePosition
  85. # switch the foreground and background colors of the character that was clicked
  86. attr = newbuffer.ReadConsoleOutputAttribute(
  87. Length=1, ReadCoord=pos
  88. )[0]
  89. new_attr = attr
  90. if attr & win32console.FOREGROUND_BLUE:
  91. new_attr = (
  92. new_attr & ~win32console.FOREGROUND_BLUE
  93. ) | win32console.BACKGROUND_BLUE
  94. if attr & win32console.FOREGROUND_RED:
  95. new_attr = (
  96. new_attr & ~win32console.FOREGROUND_RED
  97. ) | win32console.BACKGROUND_RED
  98. if attr & win32console.FOREGROUND_GREEN:
  99. new_attr = (
  100. new_attr & ~win32console.FOREGROUND_GREEN
  101. ) | win32console.BACKGROUND_GREEN
  102. if attr & win32console.BACKGROUND_BLUE:
  103. new_attr = (
  104. new_attr & ~win32console.BACKGROUND_BLUE
  105. ) | win32console.FOREGROUND_BLUE
  106. if attr & win32console.BACKGROUND_RED:
  107. new_attr = (
  108. new_attr & ~win32console.BACKGROUND_RED
  109. ) | win32console.FOREGROUND_RED
  110. if attr & win32console.BACKGROUND_GREEN:
  111. new_attr = (
  112. new_attr & ~win32console.BACKGROUND_GREEN
  113. ) | win32console.FOREGROUND_GREEN
  114. newbuffer.WriteConsoleOutputAttribute((new_attr,), pos)
  115. else:
  116. newbuffer.WriteConsole(str(input_record))
  117. time.sleep(0.1)
  118. stdout.SetConsoleActiveScreenBuffer()
  119. newbuffer.Close()
  120. if free_console:
  121. win32console.FreeConsole()