0

Hello I'm trying to find a way to detect when the user press the alt+tab key combination.

What I want to do is next:

while True:
    if not "alt+tab key pressed":
        pythoncom.PumpMessages()

How can I do this?

  • You should look into PyGame to have a window and then you can get the key events. – iFlo Dec 21 '16 at 17:25
  • In many graphical environments, alt+tab is captured by the windowing system and not passed through to the app. Can you describe in what environment you are running, and what you plan to do with the alt+tab once you get it? – Robᵩ Dec 21 '16 at 17:26
  • Duplicate? http://stackoverflow.com/questions/22362076/how-to-detect-curses-alt-key-combinations-in-python – Belphegor Dec 21 '16 at 17:28
  • @rob What I'm trying to do is capture all the keys that the user press except when the alt+tab key is pressed because the program crashes when this happen. – Mauricio Avendaño Dec 21 '16 at 17:30
  • @MauricioAvendaño Ummm... have you thought about identifying and fixing the root cause instead of trying to work around the trigger? I agree with Rob, it might be even impossible for you to capture it. – luk32 Dec 21 '16 at 17:35
  • I tried to find a solution without success, the problem is in the pythoncom.PumpMessages() function, it crashes sometimes when the alt+tab key is pressed. – Mauricio Avendaño Dec 21 '16 at 17:41
  • http://stackoverflow.com/questions/41203706/script-in-python-crashed-when-alttab-is-pressed-repeatedly-pyhook-and-pythonco – Mauricio Avendaño Dec 21 '16 at 17:42

1 Answers1

0

in order to tackle this problem I've created this piece of code, I suggest using the time.sleep function to slow down the input gathering as it can be quite resource intensive.

import time
import keyboard


alt_pressed = False

while True:  # making an infinite loop
    try:
        if keyboard.is_pressed('alt'): 
            alt_pressed = True
            print('You Pressed Alt')
        if keyboard.is_pressed('tab') and alt_pressed: 
            print('You Pressed Alt+Tab')
            break  # finishing the loop
        alt_pressed = False 
        time.sleep(0.05)
    except:
        break