python-3.xwindowspython-winshell

How to use Win_Shell to copy paste


I am writing a code which needs enter, copying and pasting:

so far I have managed to do the enter with this

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("{ENTER}", 0)

is there a way to do the same for ctrl c and v

pyperclip has not worked well but this has magnifiecently so I just wanted to ask.


Solution

  • The answer is use pynput:

    Here is something I did. w/ if statement

    from pynput.keyboard import Key,Controller
    
    
            if tm == b'RickRoll':
    
                keyboard = Controller()
                while True:
                    os.startfile("Rick Astley - Never Gonna Give You Up (Video).mp4")
    
                    while True:
                        keyboard.press(Key.media_volume_up)
                        keyboard.release(Key.media_volume_up)
                        time.sleep(0.1)
                   
    
                    if tm == 'Stop':
                        keyboard.press(Key.ctrl)
                        keyboard.press(Key.alt)
                        keyboard.press(Key.delete)
    
                        keyboard.release(Key.ctrl)
                        keyboard.release(Key.alt)
                        keyboard.release(Key.delete)
    
                        keyboard.press(Key.)
                        keyboard.release(Key.media_volume_up)
    
    
                        keyboard.press(Key.media_volume_up)
                        keyboard.release(Key.media_volume_up)
    
    
    
                        keyboard.press(Key.enter)
                        keyboard.release(Key.enter)