python win32com events don't work from command line - python

import win32com.client as cc
class AgentEvents(object):
def OnClick(self, cid, button, shift, x, y):
peedy.Speak('He he he')
def OnCommand(self, UserInput):
cmdname = cc.Dispatch(UserInput).Name
print(cmdname)
if cmdname == 'hwru':
peedy.Speak('Well. Thank you.')
elif cmdname == 'yoname':
peedy.Speak('My name is Peedy.')
ag = cc.DispatchWithEvents('Agent.Control.2', AgentEvents)
ag.Connected = True
ag.Characters.Load('peedy', 'peedy.acs')
peedy = ag.Characters('peedy')
peedy.Show()
peedy.LanguageID=0x0409
#print(peedy.SRModeID)
peedy.Commands.Add('hwru','how are you','how are you',True,True)
peedy.Commands.Add('yoname',"what's your name","what's your name",True,True)
ag.CommandsWindow.Visible=True
When I run this from pythonwin IDE all works normal, but from windows command line OnCommand events don't work, it prints no errors, COM object methods (like peedy.Speak("Hello")) work normal too.
Is it possible to use win32com activex objects with events from command line?
Solved with:
while True:
# Pump messages so that COM gets a look in
pythoncom.PumpWaitingMessages()

Related

Python PyWin32 send keys to DirectX game in the background

I have made a script that can send keys to any app in the background, but it does not work on DirectX.
I am trying to make my character jump in a game called "roblox".
This is the script
import time
import psutil
import win32con
from win32 import win32gui
from win32 import win32api
from win32 import win32process
keyDict = {" ": 0x20}
for i in range(0x41, 0x5A+1):
keyDict[chr(i)] = i
def CloseExe(exeName):
ID2Handle={}
def get_all_hwnd(hwnd,mouse):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
nID=win32process.GetWindowThreadProcessId(hwnd)
del nID[0]
for abc in nID:
try:
pro=psutil.Process(abc).name()
except psutil.NoSuchProcess:
pass
else:
if pro == exeName:
win32gui.PostMessage(hwnd,win32con.WM_CLOSE,0,0)
win32gui.EnumWindows(get_all_hwnd, 0)
def SendKeysSensitive(exeName, keysToSend):
mID2Handle={}
def get_all_hwnd(hwnd,mouse):
hwndTwo = win32gui.GetWindow(hwnd, win32con.GW_CHILD)
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
nID=win32process.GetWindowThreadProcessId(hwnd)
del nID[0]
for abc in nID:
try:
pro=psutil.Process(abc).name()
except psutil.NoSuchProcess:
pass
else:
if pro == exeName:
for key in keysToSend:
win32gui.PostMessage(hwndTwo, win32con.WM_CHAR, key, 0)
def SendKeys(exeName, keysToSend):
mID2Handle={}
def get_all_hwnd(hwnd,mouse):
hwndTwo = hwnd
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
nID=win32process.GetWindowThreadProcessId(hwnd)
del nID[0]
for abc in nID:
try:
pro=psutil.Process(abc).name()
except psutil.NoSuchProcess:
pass
else:
if pro == exeName:
for key in keysToSend:
win32gui.PostMessage(hwndTwo, win32con.WM_CHAR, key, 0)
win32gui.EnumWindows(get_all_hwnd, 0)
def TranslateKeys(text):
outKeys = []
for char in text:
outKeys.append(keyDict.get(char.upper()))
return outKeys
def ExE(name):
return name
if __name__ == '__main__':
prog = ExE("RobloxPlayerBeta.exe")
jump = TranslateKeys(" ")
SendKeys(prog, jump)
It does not work due to "RobloxPlayerBeta.exe" being DirectX.
I can not open the app, it 100% needs to be in the background.
Is this possible?
Well I had a problem similar to this and I found a solution. I'm not sure if this is what you're looking for, but there is a library for sending input to DirectX applications
It's called PyDirectInput. It's essentially PyAutoGui but works with DirectX
pip install pydirectinput
As far as roblox, I have used this before and can say it does work.
source code

Run Script as parallel Subprocess

i'm pretty new to python, so my knowledge is quiet basic. (i'm a system engineer)
i have a raspberry pi, an led strip and a python script to simulate a fire on the led strip :D
now i want to start the script by pressing my flic button. i found the fliclib sdk on github and installed it. my problem is now, how to handle the event correctly. i successfully can start the script, but i'd like to stop it by doublepress the flic button. but it seems like i'm stuck in the fire.py script as soon as i press the button once. can anybody help me how to set this up correctly please? :-)
Edit after suggestion:
i just edited my scripts as the following. i can see when the button is pressed once or twice with this output:
Starting Fire
Stopping Fire
but the led wont turn on, seems like, fire.py isn't opened or something like that.. when i set button=1 in fire.py itself, the fire turns on.
main.py
#!/usr/bin/env /usr/bin/python3
# -*- coding: utf-8 -*-
import flicbutton
import fire
button = 0
flicbutton.py
#!/usr/bin/env /usr/bin/python3
# -*- coding: utf-8 -*-
import fliclib
client = fliclib.FlicClient("localhost")
MyButton1 = '80:e4:da:71:83:42' #turquoise flic button
def got_button(bd_addr):
cc = fliclib.ButtonConnectionChannel(bd_addr)
cc.on_button_single_or_double_click_or_hold = some_handler
cc.on_connection_status_changed = \
lambda channel, connection_status, disconnect_reason: \
print(channel.bd_addr + " " + str(connection_status) + (" " + str(disconnect_reason) if connection_status == fliclib.ConnectionStatus.Disconnected else ""))
client.add_connection_channel(cc)
def got_info(items):
print(items)
for bd_addr in items["bd_addr_of_verified_buttons"]:
got_button(bd_addr)
def some_handler(channel, click_type, was_queued, time_diff):
if channel.bd_addr == MyButton1:
try:
if click_type == fliclib.ClickType.ButtonSingleClick:
print("Starting Fire")
button=1
if click_type == fliclib.ClickType.ButtonDoubleClick:
print("Stopping Fire")
button=2
if click_type == fliclib.ClickType.ButtonHold:
print("ButtonHold has not been assigned an action")
except Exception:
import datetime
print('An error occured: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
client.get_info(got_info)
client.on_new_verified_button = got_button
client.handle_events()
fire.py
import RPi.GPIO as GPIO
import threading
import time
import random
import math
R = 17
G = 22
pwms = []
intensity = 1.0
def initialize_gpio():
GPIO.setmode(GPIO.BCM)
GPIO.setup([17,22], GPIO.OUT)
def red_light():
p = GPIO.PWM(R, 300)
p.start(100)
pwms.append(p)
while True:
p.ChangeDutyCycle(min(random.randint(50, 100) * math.pow(intensity + 0.1, 0.75), 100) if intensity > 0 else 0)
rand_flicker_sleep()
def green_light():
global green_dc
p = GPIO.PWM(G, 300)
p.start(0)
pwms.append(p)
while True:
p.ChangeDutyCycle(random.randint(5, 10) * math.pow(intensity, 2) if intensity > 0 else 0)
rand_flicker_sleep()
def rand_flicker_sleep():
time.sleep(random.randint(3,10) / 100.0)
def fan_the_flame(_):
global intensity
intensity = min(intensity + 0.25, 1.0)
def light_candle():
threads = [
threading.Thread(target=red_light),
threading.Thread(target=green_light),
## threading.Thread(target=burning_down)
]
for t in threads:
t.daemon = True
t.start()
for t in threads:
t.join()
def startfire():
try:
initialize_gpio()
print("\nPress ^C (control-C) to exit the program.\n")
light_candle()
except KeyboardInterrupt:
pass
finally:
for p in pwms:
p.stop()
def stopfire():
GPIO.cleanup()
#if __name__ == '__main__':
# main()
if button == 1:
startfire()
if button == 2:
stopfire()
Have a common (global variable) that both codes can read, you can put this in a standalone file that both codes can access. So script 1 updates this variable like
if(single press): variable=1
elif(double press): variable=2
then in fire.py you can poll the variable.
if(varaible==1): start/stop fire
elif(variable==2): stop/start fire
else: #throw error
I'm sure there are more efficient ways to do this, but this method should be the easiest to understand.

Tkinter '.after()' help, GUI window doesn't open

I'm trying to make an program for twitch, where the twitch chat can vote by typing a command ("!voteA") for example, and the chatbot-code will count the votes, and show them in a GUI window, I've already gotten pretty far, but my GUI window is not opening and I think it has something to do with the self.after(10, self.get_votes) line, could anyone help me out?
Here's the code: (it has some more tabs like Read, Socket, Initialize, Settings, but I don't think they're relevant for now, if they are I can also post them)
import Tkinter as tk
from Read import getUser, getMessage
from Socket import openSocket, sendMessage
from Initialize import joinRoom
from collections import OrderedDict
class App(tk.Frame): #Toplevel is a frame with a specific use. Unless there is another window, you want a Frame.
def __init__(self):
options = [
"voteA",
"voteB",
"voteC"]
self.s = openSocket()
joinRoom(self.s)
self.readbuffer = ""
tk.Frame.__init__(self)
self['background']='black'
self.pack()
self.master.geometry("+200+200")
# self.master.attributes("-alpha", 0.7)
self.labels = OrderedDict()
for option in options:
label = tk.Label(
self,
bg="black",
fg="white",
font="HouseSlant-Regular 30",
anchor="w")
label.pack()
self.labels[option] = [0, label]
self.update_text()
self.after(10, self.get_votes)
def update_text(self):
for name, data in self.labels.items():
count, label = data
label['text'] = "{} has {} votes".format(name, count)
def get_votes(self):
self.after(10, self.get_votes)
self.readbuffer = self.readbuffer + self.s.recv(1024)
temp = self.readbuffer.split('\n')
self.readbuffer = temp.pop() #save the last (possibly incomplete) line for later
if self.readbuffer == "":
pass #no further messages in the queue
#you should add a sleep time here for the sake of your hot NIC
for line in temp:
print(line)
if "PING" in line:
s.send("PONG :tmi.twitch.tv\r\n".encode())
break
user = getUser(line)
message = getMessage(line)
print "{} typed: {}".format(user, message)
if "!commands" in message:
sendMessage(self.s, " ".join(["!"+option for option in self.labels]))
break
for option in self.labels:
if "!"+option in message:
self.labels[option][0] += 1
print self.labels[option][0]
self.update_text()
app=App()
app.mainloop()
What happens when I run the code, is that the chatbot works; if I type "!commands" or "!voteA" it will respond to that nicely, however, the GUI window isn't opening. I can see in my dock (I'm a mac user) that it's trying to open a window, but it freezes and then I have to force quit it. It's not giving an error in the compiler though. Does anyone have any idea what I've messed up?

python run a scapy function as a background process

I would like to know in any way that i can run a function as a background process.
I have gone through reading threading but i am still unclear on how to implement it.
In my system , the scapy detection script run when i click on the Button.But then the system will be hang as the scapy function is running.
What i want to achieve is that when i click on the button that initiate the scapy detection script , i would like to be able to do other function in the same system.(general idea was to prevent the system from hang)
def startMonitor(self,event):
selectedInterface = self.interfaces_cblist.GetValue()
#selectInterfaceStr = str(selectedInterface)
if len(selectedInterface) == 0:
noInterfaceSelected = wx.MessageDialog(None,"Please select an interface","",wx.ICON_ERROR)
noInterfaceSelected.ShowModal()
else:
#confirmMonitor = wx.MessageDialog(None,"Monitoring Start on %s interface"%selectedInterface,"",wx.OK)
#confirmMonitor.ShowModal()
x = selectedInterface
thread.start_new_thread(self.camtableDetection(x))
def camtableDetection(self,a):
global interface
interface = str(a)
THRESH=(254/4)
START = 5
def monitorPackets(p):
if p.haslayer(IP):
hwSrc = p.getlayer(Ether).src
if hwSrc not in hwList:
hwList.append(hwSrc)
delta = datetime.datetime.now() - start
if((delta.seconds > START) and ((len(hwList)/delta.seconds) > THRESH)):
camAttackDetected = wx.MessageDialog(None,"Cam Attack Detected","",wx.ICON_ERROR)
camAttackDetected.ShowModal()
hwList = []
start = datetime.datetime.now()
sniff(iface=interface,prn=monitorPackets)

Terminal like app in PySide

I am making an application in PySide and I want to add a Console/Terminal like screen, where you have a prompt and you can type commands. How would I be able to accomplish this. I am guessing some combination of QPlainTextEdit/QTextEdit for the output and QLineEdit for the actual prompt. Is there a better way to do this?
You could check out Spyder. They use PyQt (which is similar) and have a terminal. I think you can import their terminal widget, but I haven't played with it.
https://code.google.com/p/spyderlib/
Also, it is my favorite python editor by far!
I've spent a fair deal of time trying to find something like this, but to no avail. Good luck!
I did this with a custom QPlainTextEdit and custom QLineEdit. I also had an indicator label that would display ">>>" on the terminal to show user input. It needs more work. The best way would be to create your own custom widget based off of a QTextEdit and your own io handler. Below is an example of my execute method with self.input being the QLineEdit and self.view being the QTextEdit. It should get you the general idea.
import io, subprocess, shlex, code, rlcompleter, platform
def execute(self, currentText=None):
"""Execute runs the command given based on the console type.
If the console type is "both" then execute will run python commands
unless the user give the input ::os::command or ("::(platform.syste())::command").
Otherwise the console type will determine the what the input will execute with.
Args:
currentText(str): Text to execute. None will run the text from the QLineEdit self.input.
"""
# Check for given text
if currentText is None:
currentText = self.input.text()
self.input.clear()
self.view.display(currentText, "input")
else:
cmd = re.search("^>>>", currentText) # search for start only
if cmd is None:
currentText = ">>>" + currentText
else:
self.view.display(currentText, "input")
# end
# Variables
self.completer.clear()
cmdString = re.sub("^>>>", "", currentText)
result = None
displayType = "output"
run, cmdString = self.getRunType(cmdString)
try:
# Check where the output is going
sys.stdout = self.out = io.StringIO()
sys.stderr = sys.stdout
if run == "python": # Run python command
result = self._runInterpreter(cmdString)
displayType = "python"
elif run == "os": # Run os command
result = self._runSubprocess(cmdString)
displayType = "os"
except Exception as err:
result = str(err)
displayType = "Error"
notFoundPython = "NameError:" in result and "is not defined" in result
notFoundWindows = "returned non-zero exit status" in result
if notFoundPython or notFoundWindows:
result = "Command not found"
finally:
sys.stdout = self.old_stdout
sys.stderr = self.old_stdout
self.display(result, displayType)
# end execute
def getRunType(self, cmdString):
run = self._consoleType
# Check the run type
if self._consoleType == "both":
if re.search("^::python::", cmdString) is not None:
cmdString = re.sub("^::[a-z]*::", "", cmdString)
run = "python"
elif re.search("^(::os::|::"+platform.system()+"::)", cmdString) is not None:
cmdString = re.sub("^::[a-z]*::", "", cmdString)
run = "os"
else:
run = "python"
# end
return run, cmdString
# end getRunType
def _runInterpreter(self, cmdString, outIO=None, run=None):
# Check for a proper console type
if(self._consoleType != "both" and self._consoleType != "python"):
return
# Get the IO
if outIO is None:
outIO = sys.stdout
# Run python command
self.interpreter.push(cmdString)
# Check outIO
result = "Unreadable buffer: Check python's sys.stdout"
if isinstance(outIO, io.StringIO):
result = outIO.getvalue()
else:
if outIO.readable():
result = str(outIO.readlines())
# Check for error
if re.search("^Traceback", result) or re.search("[a-zA-z]*Error:", result):
raise ValueError(result)
return result
# end _runInterpreter
def _runSubprocess(self, cmdString, run=None):
# Check for a proper console type
if(self._consoleType != "both" and self._consoleType != "os"):
return
# Run OS command
cmd = shlex.split(cmdString)
result = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT).decode("utf-8")
# Check for error
if re.search("^Traceback", result) or re.search("[a-zA-z]*Error:", result):
raise ValueError(result)
return result
# end _runSubprocess

Categories