2

I'm a bit of a noob, so sorry in advance if I'm not doing this right.

I'm using python 3.6x

What I have so far that gets the command window open is

import os
os.open("start cmd")

but this comes up in the directory that I'm working from and not in administration mode.

I've also tried

import os
os.system("start /wait cmd /wait {tskill dwm}")

but that didn't work either. (tskill dwm is what I'm trying to get python to write in to command manager to fix a bug with windows' buttons not going away)

My overall goal is to just click this python script Blah.py and have it restart the windows viewer or whatever it's called. Doing this clears the stuck buttons. Overall this is just an exercise in practicing python. I know I could just disable the button fade out and that would take care of the issue. I just figured this would be a good learning opportunity.

IcanCwhatUsay
  • 81
  • 1
  • 1
  • 8
  • Possible duplicate of [open cmd with admin rights (Windows 10)](https://stackoverflow.com/questions/34500369/open-cmd-with-admin-rights-windows-10) – ewcz Mar 02 '18 at 15:55
  • Does this answer your question? [Request UAC elevation from within a Python script?](https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script) – Evtim Kostadinov Oct 15 '21 at 09:17

2 Answers2

1
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()

keyboard.press(Key.cmd)
keyboard.release(Key.cmd)
time.sleep(0.3)

keyboard.type("cmd")
time.sleep(1)
keyboard.press(Key.right)
keyboard.release(Key.right)
keyboard.press(Key.down)
keyboard.release(Key.down)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(0.5)
keyboard.press(Key.tab)
keyboard.release(Key.tab)
keyboard.press(Key.tab)
keyboard.release(Key.tab)
keyboard.press(Key.enter)
keyboard.release(Key.enter)

exit
Psysmar
  • 11
  • 2
0

The answer is here

https://stackoverflow.com/a/11746382/7352806

import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
    shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    sys.exit(0)
Narendra
  • 1,511
  • 1
  • 10
  • 20