-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
남박사님 함수가 호출이 안되네요.
20.04.06 15:02 작성 조회수 103
1
나만의 단축키 smart 강좌를 보고 따라 쳐봤는데, 키 인식은 잘되나 function1,2,3 함수가 호출이 되지 않아요.
무엇이 틀렸을까요?
from pynput.keyboard import Key, Listener, KeyCode
import win32api
# 단축키 저장
MY_HOT_KEYS =[
{"function1": {Key.ctrl_l, Key.alt_l, KeyCode(char="n")}},
{"function2": {Key.shift, Key.ctrl_l, KeyCode(char="b")}},
{"function3": {Key.shift, Key.ctrl_l, KeyCode(char="g")}},
]
# 키가 눌러져있는 키 상태를 기억하는 변수(집합)
current_keys = set()
def function1():
print("함수1 호출")
win32api.WinExec("calc.exe")
def function2():
print("함수2 호출")
win32api.WinExec("notepad.exe")
def function3():
print("함수3 호출")
win32api.WinExec("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
def key_pressed(key):
print("Pressed {}".format(key))
for data in MY_HOT_KEYS:
FUNCTION = list(data.keys())[0]
KEYS = list(data.values())[0]
if key in KEYS:
current_keys.add(key)
if all(k in current_keys for k in KEYS):
#checker = True
#for k in KEYS:
#if k not in current_keys:
#checker = False
#break
#if checker:
function = eval(FUNCTION)
function()
def key_released(key):
print("Released {}".format(key))
if key in current_keys:
current_keys.remove(key)
if key == Key.esc:
return False
# on_press, on_release는 인자값
with Listener(on_press=key_pressed, on_release=key_released) as Listener:
Listener.join()
답변을 작성해보세요.
0
남박사
지식공유자2020.04.07
from pynput.keyboard import Key, Listener, KeyCode
import win32api
# 단축키 저장
MY_HOT_KEYS =[
{"function1": {Key.ctrl_l, Key.alt_l, KeyCode(char="n")}},
{"function2": {Key.shift, Key.ctrl_l, KeyCode(char="b")}},
{"function3": {Key.shift, Key.ctrl_l, KeyCode(char="g")}},
]
# 키가 눌러져있는 키 상태를 기억하는 변수(집합)
current_keys = set()
def function1():
print("함수1 호출")
win32api.WinExec("calc.exe")
def function2():
print("함수2 호출")
win32api.WinExec("notepad.exe")
def function3():
print("함수3 호출")
win32api.WinExec("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
def key_pressed(key):
print("Pressed {}".format(key))
for data in MY_HOT_KEYS:
FUNCTION = list(data.keys())[0]
KEYS = list(data.values())[0]
if key in KEYS:
print("등록된 키 확인 {}".format(key))
current_keys.add(key)
if all(k in current_keys for k in KEYS):
function = eval(FUNCTION)
function()
def key_released(key):
print("Released {}".format(key))
if key in current_keys:
current_keys.remove(key)
if key == Key.esc:
return False
# on_press, on_release는 인자값
with Listener(on_press=key_pressed, on_release=key_released) as Listener:
Listener.join()
위의 코드를 그대로 복사해서 실행해본 결과 제 컴퓨터에서는 아무 문제 없이 동작하는걸 확인했습니다.
어떤 키를 인식하지 못하는지부터 확인하시어 해결해보셔야 할듯 합니다만 아마도 사용하시는 컴퓨터의 키 코드값이 다를거라 예상되는데 등록하신 n, b, g 등의 키값이 제대로 찍히는지 확인해보시기 바랍니다.
답변 1