2016-10-21 2 views
0

나는 Tkinter에서 동시에 키를 눌러야하는 게임을하고 싶었을 때 빠르게 빠져 들었습니다. 그래서 사용자가 모든 활성 키를 가져올 수있는 위젯을 만들기로 결정한 것입니다.많은 키를 동시에 바인딩

메도은 간단하다 : 나는 일련의 키를 누를

  • 을했습니다

    • , 내가 키를 놓을 때
    • , 나는 세트
    • 에서 폐기 세트에 추가 사용자가 누른되는 키를 알고 싶다면
    • , 난 그냥

    나는이 코드를 수행 한 집합을 반환 :

    import tkinter as tk 
    
    class Tk_Multikey(tk.Frame): 
        """Allow the user to get all actived keys""" 
    
        def __init__(self, master, **kwargs): 
         super().__init__(master, **kwargs) 
    
         self.keysym = set() 
         self.keycode = set() 
         self.keysym_num = set() 
         master.bind('<KeyPress>', self.add_a_key, add='+') 
         master.bind('<KeyRelease>', self.discard_a_key, add='+') 
    
        def add_a_key(self, event): 
         """When a key is pressed, add its keysym, keycode and keysym_num to the corresponding set""" 
    
         self.keysym.add(event.keysym.upper()) # the upper method prevents a 
         # bug when the user is using the Shift/Caplock key and a letter key 
         self.keycode.add(event.keycode) 
         self.keysym_num.add(event.keysym_num) 
    
    
        def discard_a_key(self, event): 
         """When a key is released, discard its keysym, keycode and keysym_num to the corresponding set""" 
    
         self.keysym.discard(event.keysym.upper()) 
         self.keycode.discard(event.keycode) 
         self.keysym_num.discard(event.keysym_num) 
    
    
        def __delattr__(self, nameattr): 
         """The user is not allowed to delete a set""" 
         raise AttributeError("Unable to delete") 
    
        def get_keys(self): 
         return((self.keysym, self.keycode, self.keysym_num)) 
    
    
    if __name__ == "__main__": 
    
        def update(event=None): 
         print("updating") 
         strvar_keysym.set(mtk.keysym) 
         strvar_keycode.set(mtk.keycode) 
         strvar_keysym_num.set(mtk.keysym_num) 
    
        root = tk.Tk() 
    
        mtk = Tk_Multikey(root) 
        mtk.pack() 
    
        root.bind('<KeyPress>', update, add='+') 
        root.bind('<KeyRelease>', update, add='+') 
    
        strvar_keysym = tk.StringVar() 
        strvar_keycode = tk.StringVar() 
        strvar_keysym_num = tk.StringVar() 
        update() 
    
        tk.Label(root, textvariable=strvar_keysym).pack()  
        tk.Label(root, textvariable=strvar_keycode).pack()  
        tk.Label(root, textvariable=strvar_keysym_num).pack() 
    
        root.mainloop() 
    

    이제 버그가 있습니다.

    내가 A, Z, E, R을 누르면 작동합니다. 그러나 내가 A, Z, E, R T를 누르면, 그것은 작동하지 않습니다 (T는 나타나지 않습니다). 그러나, 만약 내가 오직 A, Z, E, T만을 누르면, 그것은 작동합니다. 나는 최대가 4라고 믿었지만 많은 다른 열쇠로 시도했지만 그게 아닙니다. (예를 들어 W, X, C와 같은 버그가 있습니다)

    왜이 버그가 발생하며 어떻게 해결할 수 있습니까? ... 난 내 메시지의 시작 부분에 인사 수없는 이유를 모르겠어요 그래서 나는 그것을 끝을 넣어 :

    는 :

    PS 감사합니다 안녕하세요!

  • 답변

    0

    가능한 대답 here. 특히 this n 키 롤오버에서 위키 백과 페이지.

    기본적으로 키보드는 동일한 회로에서 통신하기 때문에 일정량의 키를 사용할 수 없습니다. 하드웨어 제한 사항입니다. 서로 다른 키 조합을 매핑하여 어떤 키 조합이 함께 작동하는지 확인하십시오.

    +0

    답장을 보내 주셔서 감사합니다. 내 생각 엔 당신이 맞다. 나는 (당신의 위키 피 디아 페이지에 따라) 작동하지 않는 몇 가지 조합을 시험해 보았습니다. 그리고 그것은 작동하지 않습니다. 그래, 그게 그랬어. ^^ 모두에게 감사합니다. 문제 해결됨 :) – WexyR

    관련 문제