win32console_demo.py 4.4 KB

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