如何在Windows上使用3个参数创建全局热键? [英] How to create a global hotkey on Windows with 3 arguments?

查看:176
本文介绍了如何在Windows上使用3个参数创建全局热键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像Ctl,Alt +删除

Just like Ctl, Alt + delete

我想编写一个程序,该程序在python中使用带有3个或更多参数的全局热键.仅当我按下键盘上的所有三个键时,分配的功能才应执行.例如alt,windows和F3.

I want to write a program, which uses global hotkeys with 3 or more arguments in python. The assigned function should only perform when I press all three keys on my keyboard. For example alt, windows and F3.

win32con.VK_F3, win32con.MOD_WIN, win32con.VK_F5

这是我要运行的当前程序,但是其输出是:

This is the current program I want to run, however its output is:

Traceback (most recent call last):
 File "C:\Python32\Syntax\hot keys\hotkeys2.py", line 41, in <module>
   for id, (vk, modifiers) in HOTKEYS.items ():
ValueError: too many values to unpack (expected 2)

程序:

import os
import sys
import ctypes
from ctypes import wintypes
import win32con

byref = ctypes.byref
user32 = ctypes.windll.user32

HOTKEYS = {
  1 : (win32con.VK_F3, win32con.MOD_WIN, win32con.VK_F5),
  2 : (win32con.VK_F4, win32con.MOD_WIN),
  3 : (win32con.VK_F2, win32con.MOD_WIN)
    }

    def handle_win_f3 ():
  #os.startfile (os.environ['TEMP'])
  print ("Hello WOrld! F3")

def handle_win_f4 ():
  #user32.PostQuitMessage (0)
    print ("Hello WOrld! F4")

def handle_win_f1_escape ():
    print("exit")
    sys.exit()

HOTKEY_ACTIONS = {
  1 : handle_win_f3,
  2 : handle_win_f4,
  3 : handle_win_f1_escape
}

for id, (vk, modifiers) in HOTKEYS.items ():
  print ("Registering id", id, "for key", vk)
  if not user32.RegisterHotKey (None, id, modifiers, vk):
    print ("Unable to register id", id)

try:
  msg = wintypes.MSG ()
  while user32.GetMessageA (byref (msg), None, 0, 0) != 0:
    if msg.message == win32con.WM_HOTKEY:
      action_to_take = HOTKEY_ACTIONS.get (msg.wParam)
      #print(" msg.message == win32con.WM_HOTKEY:")
      if action_to_take:
        action_to_take ()

    user32.TranslateMessage (byref (msg))
    user32.DispatchMessageA (byref (msg))

finally:
  for id in HOTKEYS.keys ():
    user32.UnregisterHotKey (None, id)
    print("user32.UnregisterHotKey (None, id)")

注册3个热键?有可能吗? 说明一个人如何使用,分配一个需要按下的键,然后分配其中两个是否需要按下的键.但是,我不会说该功能仅在同时按下所有功能时才执行.我拿了

Registering 3 hotkeys? Possible? Explains how one can use assign one key that needs to be pressed and then if two of which either needs to be pressed. However I won’t that the function only performs when all there are pressed simultaneously. I took

推荐答案

对于初学者来说,如果您需要Alt,Windows和F3,是否不需要在HOTKEYS条目中使用win32con.VK_F3, win32con.MOD_ALT, win32con.MOD_WIN?

For starters, if you wanted alt, windows and F3, wouldn't you need to use win32con.VK_F3, win32con.MOD_ALT, win32con.MOD_WIN for the HOTKEYS entry?

但是,用 Win F5 F3 并没有真正意义>键.

However, it doesn't really make sense to say press F3 with modifiers of the Win and F5 key.

在线错误:

for id, (vk, modifiers) in HOTKEYS.items ():

是因为每个字典条目的值都是可变长度tuple.这是一种对所有修饰符值进行按位 OR 进行按位运算的方法,以准备将它们作为单个参数传递给RegisterHotKey().

is because the value of each dictionary entry is a variable length tuple. Here's a way to handle that which also bitwise OR s all the modifier values together in preparation for passing them as a single argument to RegisterHotKey().

from functools import reduce

for id, values in HOTKEYS.items ():
    vk, modifiers = values[0], reduce (lambda x, y: x | y, values[1:])
    print ("Registering id", id, "for key", vk)
    if not user32.RegisterHotKey (None, id, modifiers, vk):
        print ("Unable to register id", id)

如果您的代码正确缩进并遵循 PEP 8-Python代码样式指南建议.请考虑在将来这样做.

It would have been easier to work on your problem if your code was indented properly and followed the PEP 8 -- Style Guide for Python Code recommendations. Please consider doing so in the future.

这篇关于如何在Windows上使用3个参数创建全局热键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆