How to send Alt+space to console window?

試著忘記壹切 提交于 2019-12-02 11:22:10

问题


import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("Command Prompt")
shell.SendKeys("%{ }")               # This
#shell.SendKeys("% ")                # and this
#shell.SendKeys("%( )")              # and this is not working

WScript is not mandatory, you can propose any programmatic solution.


回答1:


Scan codes (56 and 57) are required:

from win32api import *
from win32con import *
keybd_event(VK_LMENU, 56, 0, 0)
keybd_event(VK_SPACE, 57, 0, 0)
keybd_event(VK_SPACE, 57, KEYEVENTF_KEYUP, 0)
keybd_event(VK_LMENU, 56, KEYEVENTF_KEYUP, 0)



回答2:


I think it is a bug in windows. Here is an example where in notepad the "% " works and in CMD it does not:

    ' SendKeys.vbs  Example Script
    ' VBScript to send keyboard strokes to command prompt and notepad
    ' --------------------------------------------------------'
    Option Explicit

    Dim objShell, WshShell
    set objShell = CreateObject("WScript.Shell")
    objShell.Run("cmd")
    objShell.AppActivate("Command Prompt")
    WScript.Sleep 500
    objShell.SendKeys "notepad~"
    WScript.Sleep 500
    ' It works
    objShell.SendKeys "% x"
    WScript.Sleep 200
    objShell.SendKeys "% r"
    WScript.Sleep 200
    objShell.SendKeys "%v"
    WScript.Sleep 200
    objShell.SendKeys "s"
    WScript.Sleep 200
    objShell.SendKeys "Algo{Enter}"
    WScript.Sleep 500
    objShell.SendKeys "%{TAB}"
    WScript.Sleep 500
    objShell.SendKeys "dir~"
    WScript.Sleep 500
    'Does not work
    objShell.SendKeys "% es"
    WScript.Sleep 3500
    objShell.SendKeys "{BACKSPACE}{BACKSPACE}Exit~"
    WScript.Sleep 3500
    objShell.SendKeys "%fx"
    WScript.Sleep 500
    objShell.SendKeys "{TAB}~"

    WScript.Quit
    '  End of SendKeys Example Script


来源:https://stackoverflow.com/questions/7023509/how-to-send-altspace-to-console-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!