windows下使用python发送控制键

在windows下使用python进行图形类操作时,经常需要用到发送快捷键的情况,如ALT+TAB、crtl + F1、Fn + f1等之类的操作,利用win32com模块,可以很便利的进行该类操作。如下一个示例是打开Outlook程序,获取第一个元素,并将其内容复制到粘贴板的示例:



<br />
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("outlook")
shell.AppActivate("Outlook")
shell.SendKeys("^o", 0)
shell.SendKeys("^a", 0)
shell.SendKeys("^c", 0)
部分键映射关系如下:



<table border="1" height="707" width="331">
    <tbody>
        <tr>
            <td>
                <p>
                    <strong>Key</strong>

            </td>
            <td>

                    <strong>Argument</strong>

            </td>
        </tr>
        <tr>
            <td width="39%">

                    BACKSPACE

            </td>
            <td width="61%">

                    {BACKSPACE}, {BS}, or {BKSP}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    BREAK

            </td>
            <td width="61%">

                    {BREAK}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    CAPS LOCK

            </td>
            <td width="61%">

                    {CAPSLOCK}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    DEL or DELETE

            </td>
            <td width="61%">

                    {DELETE} or {DEL}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    DOWN ARROW

            </td>
            <td width="61%">

                    {DOWN}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    END

            </td>
            <td width="61%">

                    {END}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    ENTER

            </td>
            <td width="61%">

                    {ENTER} or ~

            </td>
        </tr>
        <tr>
            <td width="39%">

                    ESC

            </td>
            <td width="61%">

                    {ESC}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    HELP

            </td>
            <td width="61%">

                    {HELP}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    HOME

            </td>
            <td width="61%">

                    {HOME}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    INS or INSERT

            </td>
            <td width="61%">

                    {INSERT} or {INS}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    LEFT ARROW

            </td>
            <td width="61%">

                    {LEFT}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    NUM LOCK

            </td>
            <td width="61%">

                    {NUMLOCK}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    PAGE DOWN

            </td>
            <td width="61%">

                    {PGDN}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    PAGE UP

            </td>
            <td width="61%">

                    {PGUP}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    PRINT SCREEN

            </td>
            <td width="61%">

                    {PRTSC}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    RIGHT ARROW

            </td>
            <td width="61%">

                    {RIGHT}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    SCROLL LOCK

            </td>
            <td width="61%">

                    {SCROLLLOCK}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    TAB

            </td>
            <td width="61%">

                    {TAB}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    UP ARROW

            </td>
            <td width="61%">

                    {UP}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F1

            </td>
            <td width="61%">

                    {F1}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F2

            </td>
            <td width="61%">

                    {F2}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F3

            </td>
            <td width="61%">

                    {F3}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F4

            </td>
            <td width="61%">

                    {F4}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F5

            </td>
            <td width="61%">

                    {F5}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F6

            </td>
            <td width="61%">

                    {F6}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F7

            </td>
            <td width="61%">

                    {F7}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F8

            </td>
            <td width="61%">

                    {F8}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F9

            </td>
            <td width="61%">

                    {F9}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F10

            </td>
            <td width="61%">

                    {F10}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F11

            </td>
            <td width="61%">

                    {F11}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F12

            </td>
            <td width="61%">

                    {F12}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F13

            </td>
            <td width="61%">

                    {F13}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F14

            </td>
            <td width="61%">

                    {F14}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F15

            </td>
            <td width="61%">

                    {F15}

            </td>
        </tr>
        <tr>
            <td width="39%">

                    F16

            </td>
            <td width="61%">

                    {F16}

            </td>
        </tr>
    </tbody>
</table>

几个特殊键键映射关系如下:



<table border="1">
    <tbody>
        <tr>
            <td width="39%">
                <p>
                    Key

            </td>
            <td width="61%">

                    Special Character

            </td>
        </tr>
        <tr>
            <td width="39%">

                    SHIFT

            </td>
            <td width="61%">

                    +

            </td>
        </tr>
        <tr>
            <td width="39%">

                    CTRL

            </td>
            <td width="61%">

                    ^

            </td>
        </tr>
        <tr>
            <td width="39%">

                    ALT

            </td>
            <td width="61%">

                    %

            </td>
        </tr>
        <tr>
            <td width="39%">

                    ENTER

            </td>
            <td width="61%">

                    ~

            </td>
        </tr>
    </tbody>
</table>

通过以上映射关系表,如实现ctrl + F5 强制刷新浏览器页面就比较容易实现,shell.SendKeys("^{F5}", 0) 即可。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注