Python custom shortcut keys and bat operation

Implement "ctrl+up" to increase the volume "ctrl+down" to decrease the volume

First: the code to increase the volume:

import win32api
import win32con
win32api.keybd_event(win32con.VK_VOLUME_UP, 0, 0, 0)
# 等待一段时间,确保已经触发键盘事件
time.sleep(0.1)
win32api.keybd_event(win32con.VK_VOLUME_UP, 0, win32con.KEYEVENTF_KEYUP, 0)

In general, only one line of code is required

win32api.keybd_event(win32con.VK_VOLUME_UP, 0)

However, we want to implement it through the keyboard.add_hotkey method of the keyboard library, so the next two lines of code are required

Total code:

import win32api
import win32con
import keyboard
import time
# def on_hotkey():
#     print('Hotkey pressed!')
is_volume_setup = True
def up_volume():
    # print("up_volume")
    if is_volume_setup:
        win32api.keybd_event(win32con.VK_VOLUME_UP, 0, 0, 0)
        # 等待一段时间,确保已经触发键盘事件
        time.sleep(0.1)
        win32api.keybd_event(win32con.VK_VOLUME_UP, 0, win32con.KEYEVENTF_KEYUP, 0)
    # print("增大音量")
def down_volume():
    # print("down_volume")
    if is_volume_setup:
        win32api.keybd_event(win32con.VK_VOLUME_DOWN, 0, 0, 0)
        # 等待一段时间,确保已经触发键盘事件
        time.sleep(0.15)
        win32api.keybd_event(win32con.VK_VOLUME_DOWN, 0, win32con.KEYEVENTF_KEYUP, 0)
    # print("减小音量")
def switch_volume_setup():
    global is_volume_setup
    # print("switch_volume_setup")
    is_volume_setup = False if is_volume_setup else True
    # print(is_volume_setup)


keyboard.add_hotkey('right ctrl+esc', switch_volume_setup)

keyboard.add_hotkey('right ctrl+up', up_volume)
keyboard.add_hotkey('right ctrl+down', down_volume)

keyboard.wait()

In the last line of code, if the parameter is "esc", press esc to end the operation. If no parameter is written, the operation will not end! If you don’t write this code, it will end after one run, so you have to add keyboard.wait()

keyboard.wait()

After writing the code, run it to see the effect

Implement "ctrl+up" to increase the volume "ctrl+down" to decrease the volume

And press "ctrl+esc" to switch the use of shortcut keys  

But what we want is to run the code as soon as it is turned on, and there will be no running window. Everyone knows that when running python code, a running window will appear.

First:

        Write a bat file: Create a new txt file and save it as an ANSI-encoded bat file to prevent Chinese garbled characters. In the bat file, the normal display of Chinese requires ANSI encoding. Of course, the default encoding of txt files on general computers is ANSI encoding, but my computer has set the default encoding of txt files to utf-8. So be careful! ! !

Write in the bat file:

@echo off
start /B python D:\Users\LENOVO\有意思的玩法\实现自定义快捷键\组合快捷键正式版.py

second:

        The start /B in it can be omitted. Although this function is to prevent the window from appearing, even if it is written, there will still be a secondary window. We need other ways to cancel the window.

        In order to realize that the window does not appear, we need another file VB code to run the bat file and let it run in the background. The suffix is ​​vbs

Like the bat file, remember to save it in ANSI-encoded format when saving, otherwise it will report an error saying that the file cannot be found.

       Just modify the path of the bat file! ! ! ! !

CreateObject("Wscript.Shell").Run """C:\Users\Your Name\test.bat""", 0, True

Third:
Press win+r to open the running interface:
input: shell:startup to enter the folder path of the program started at startup

Then copy the vbs file in the past

Of course, both bat and vbs files can be run directly, and you can directly see the running results, whether the shortcut keys are valid, and whether a window appears.

Finally, the memory occupied by adding this shortcut key is about 11M, which is relatively small, so don’t worry about affecting the use of the computer.

 

Guess you like

Origin blog.csdn.net/conquer_galaxy/article/details/130758178