def send_msg(self, msg): self.lock.acquire() # for this problem: https://bbs.csdn.net/topics/390973288?page=1 win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32gui.ShowWindow(self.con_hwnd, win32con.SW_MAXIMIZE) win32gui.SetForegroundWindow(self.con_hwnd) win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(msg, win32con.CF_UNICODETEXT) win32clipboard.CloseClipboard() win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x56, 0, 0, 0) # 'v' win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x0d, 0, 0, 0) # enter win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x0d, 0, win32con.KEYEVENTF_KEYUP, 0) # release key time.sleep(1) win32gui.SetForegroundWindow(self.cur_hwnd) self.lock.release()
def to_clipboard_for_excel(array, decimals=None): r"""Copy an array into a string format acceptable by Excel. It is possible to round the array. Columns separated by \t, rows separated by \r\n. From: https://stackoverflow.com/questions/22488566/how-to-paste-a-numpy-array-to-excel Parameters ---------- array : np.ndarray Array (to be rounded and) copied to the clipboard. decimals : int, optional Number of decimals of the rounded output, by default None. """ # Create a list of strings representing the array data. # The data is numerically rounded and limited accordingly in the output. line_strings = [] for row in array: temp_row = np.round(row, decimals) if decimals is not None else row formatted_line = [ f"{elmt:.{decimals}f}" if decimals is not None else str(elmt) for elmt in temp_row ] line_strings.append("\t".join(formatted_line).replace("\n", "")) # Merge the strings in one string formatted for Excel/Mike. array_string = "\r\n".join(line_strings) # Put string into clipboard (open, clear, set, close) win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(array_string) win32clipboard.CloseClipboard()
def copyToClipboard(text): ''' Copy text to clipboard. I don't care about Linux.''' text = str(text) system = platform.system() if system == 'Windows': try: from win32 import win32clipboard win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT) win32clipboard.CloseClipboard() except ModuleNotFoundError: # Solution: https://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python/ from tkinter import Tk r = Tk() r.withdraw() r.clipboard_clear() r.clipboard_append(text) r.update( ) # now it stays on the clipboard after the window is closed r.destroy() elif system == 'Darwin': process = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE) process.communicate(text.encode('utf-8'))
def isTradeAcceptable(exaltsThatShouldBeInTrade, chaosThatShouldBeInTrade): exaltsInTrade = 0 chaosInTrade = 0 tradeAcceptable = False i = 0 while exaltsInTrade < exaltsThatShouldBeInTrade or chaosInTrade < chaosThatShouldBeInTrade or i < 3: i += 1 exaltsInTrade = 0 chaosInTrade = 0 for x in range(12): for y in range(5): pag.moveTo(topLeftTrade[0] + squareSize[0] * x, topLeftTrade[1] + squareSize[1] * y) if not tradeAcceptable: pag.hotkey('ctrl', 'c') win32cp.OpenClipboard() data = win32cp.GetClipboardData() win32cp.CloseClipboard() data = data.replace('\r\n', ' ') data = data.replace('\n', ' ').replace('\r', ' ') exaltsInTrade += ctrlcToExalts(data) # Todo: What if exalted instead of chaos chaosInTrade += ctrlcToChaos(data) if exaltsInTrade >= exaltsThatShouldBeInTrade and chaosInTrade >= chaosThatShouldBeInTrade: tradeAcceptable = True break return tradeAcceptable
def send_to_clipboard(image): output = BytesIO() image.convert('RGB').save(output, 'BMP') data = output.getvalue()[14:] output.close() win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data) win32clipboard.CloseClipboard()
def get_text(): status = True win32clipboard.OpenClipboard() try: text = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT) except Exception as E: text = str(E) status = False finally: win32clipboard.CloseClipboard() return True if status else False, text
def handle_callback(event): nonlocal inp_tip_text self.inp.delete(0, END) wcb.OpenClipboard() try: inp_tip_text = wcb.GetClipboardData(win32con.CF_TEXT).decode("gb2312").strip() except (TypeError, UnicodeDecodeError) as e: print(e) wcb.CloseClipboard() if bool(self.site_detect.match(inp_tip_text)): self.inp.insert(0, inp_tip_text)
def write(data, format=wc.CF_UNICODETEXT): """write contents to clipboard. :param data: str or bytes :param int format: constants defined in winuser.h, the clipboard format. """ try: wc.OpenClipboard() wc.EmptyClipboard() wc.SetClipboardData(format, data) finally: wc.CloseClipboard()
def read(format=None) -> bytes: """read data from clipboard :param int format: the clipboard format from winuser.h, ``CF_*`` constants. if ``None``, will use first format which ``EnumClipboardFormats`` return. """ if format is None: format = ClipBoard.enum_clipformats()[0] try: wc.OpenClipboard() data = wc.GetClipboardData(format) finally: wc.CloseClipboard() return data
def copy(self): # 将截取的范围保存到剪切板 tmp_file = 'tmp.bmp' self.save_img(tmp_file) aString = windll.user32.LoadImageW(0, tmp_file, win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE) if aString != 0: win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(win32con.CF_BITMAP, aString) win32clipboard.CloseClipboard() if os.path.exists(tmp_file): os.remove(tmp_file) else: print('粘贴到剪切板失败')
def receive_msg(self): self.lock.acquire() # for this problem: https://bbs.csdn.net/topics/390973288?page=1 win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32gui.ShowWindow(self.con_hwnd, win32con.SW_MAXIMIZE) win32gui.SetForegroundWindow(self.con_hwnd) time.sleep(1) x_mid = int(win32api.GetSystemMetrics(win32con.SM_CXSCREEN) / 2) y_mid = int(win32api.GetSystemMetrics(win32con.SM_CYSCREEN) / 2) win32api.SetCursorPos([x_mid, y_mid]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0) # left mouse button win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0) # release key win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x41, 0, 0, 0) # 'a' win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x41, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x43, 0, 0, 0) # 'c' win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x43, 0, win32con.KEYEVENTF_KEYUP, 0) # release key time.sleep(1) win32clipboard.OpenClipboard() text = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT) win32clipboard.CloseClipboard() win32gui.SetForegroundWindow(self.cur_hwnd) time.sleep(1) self.lock.release() return text
def enum_clipformats() -> list: """枚举当前剪贴板中的数据格式""" formats = [] fm = 0 try: wc.OpenClipboard() while True: fm = wc.EnumClipboardFormats(fm) if fm != 0: formats.append(fm) else: break finally: wc.CloseClipboard() return formats
def loadclipboard(self, init=False): text_target = self.data[self.line_selector] if init: print(f'다음 복사할 텍스트: {text_target}') else: cb.OpenClipboard() cb.EmptyClipboard() cb.SetClipboardText(text_target) cb.CloseClipboard() print( f'{text_target}을/를 복사했습니다. | {self.line_selector + 1}/{len(self.data)}' ) if self.line_selector + 1 < len(self.data): print(f'다음 복사할 텍스트: {self.data[self.line_selector + 1]}')
def getText(self): w.OpenClipboard() d = w.GetClipboardData(win32con.CF_UNICODETEXT) w.CloseClipboard() return d
def setText(self, aStr): w.OpenClipboard() w.EmptyClipboard() d = w.SetClipboardData(win32con.CF_UNICODETEXT, aStr) w.CloseClipboard()
def getCopyText(self): wc.OpenClipboard() copy_text = wc.GetClipboardData(win32con.CF_TEXT) wc.CloseClipboard() return copy_text
def set_text(text): win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(win32con.CF_OEMTEXT, text) win32clipboard.CloseClipboard()
def main(): global buyOrders global stopThreads global playersInArea global tradeAccepted pollingThread = threading.Thread(target=pollingFunc, daemon=True) pollingThread.start() #Todo: What if already in party #Todo: What if already in area while True: if not buyOrders.empty(): currentBuyOrder = buyOrders.get() pag.moveTo(topLeftQuadStash[0] + quadSquareSize[0] * (currentBuyOrder.itemPosition[0] - 1), topLeftQuadStash[1] + quadSquareSize[1] * (currentBuyOrder.itemPosition[1] - 1)) pag.hotkey('ctrl', 'c') win32cp.OpenClipboard() data = win32cp.GetClipboardData() win32cp.CloseClipboard() data = data.replace('\r\n', ' ') data = data.replace('\n', ' ').replace('\r', ' ') isCorrectItem = currentBuyOrder.correctItem(data) if not isCorrectItem: return #time.sleep(2) pag.keyDown('ctrl') pag.click() pag.keyUp('ctrl') clearChat() win32cp.OpenClipboard() win32cp.EmptyClipboard() win32cp.SetClipboardText('/invite ' + currentBuyOrder.buyerName) win32cp.CloseClipboard() pag.hotkey('ctrl', 'v') pag.press('enter') playerGoingToAreaCounter = 0 while currentBuyOrder.buyerName not in playersInArea and playerGoingToAreaCounter < 30: #Todo: This needs a timeout playerGoingToAreaCounter += 1 print('|' + currentBuyOrder.buyerName + '| Not in area yet...') time.sleep(0.5) if currentBuyOrder.buyerName in playersInArea: print('In Area!') time.sleep(2) clearChat() win32cp.OpenClipboard() win32cp.EmptyClipboard() win32cp.SetClipboardText('/tradewith ' + currentBuyOrder.buyerName) win32cp.CloseClipboard() pag.hotkey('ctrl', 'v') pag.press('enter') tradeWindowOpened = False while not tradeWindowOpened: try: tradeWindowOpened = pag.locateOnScreen('trade.png', confidence=0.99) except pag.ImageNotFoundException: time.sleep(0.5) #time.sleep(10) pag.moveTo(1296, 614) pag.keyDown('ctrl') pag.click() pag.keyUp('ctrl') exaltsThatShouldBeInTrade = math.floor(currentBuyOrder.exalts) chaosThatShouldBeInTrade = currentBuyOrder.chaos + (currentBuyOrder.exalts - exaltsThatShouldBeInTrade) * exaltsToChaos acceptTrade = isTradeAcceptable(exaltsThatShouldBeInTrade, chaosThatShouldBeInTrade) if acceptTrade: pag.moveTo(374, 836) pag.click() i = 0 while not tradeAccepted and i < 20: #Todo this doesnt work i += 1 try: pag.locateOnScreen('countdown.png', confidence=0.99) acceptTrade = isTradeAcceptable(exaltsThatShouldBeInTrade, chaosThatShouldBeInTrade) if acceptTrade: playsound('acceptTrade.mp3') #pag.moveTo(374, 836) #pag.click() else: break except pag.ImageNotFoundException: time.sleep(0.5) tradeAccepted = False clearChat() win32cp.OpenClipboard() win32cp.EmptyClipboard() win32cp.SetClipboardText('/kick ' + currentBuyOrder.buyerName) win32cp.CloseClipboard() pag.hotkey('ctrl', 'v') pag.press('enter') if keyboard.is_pressed('q'): stopThreads = True pollingThread.join(2) return
from time import sleep, gmtime from os import mkdir from os.path import exists, expanduser def get_timestamp(): now = gmtime() return f"{now.tm_mday}-{now.tm_mon}-{now.tm_year} {now.tm_hour}-{now.tm_min}-{now.tm_sec}" bmp_header_hex = "424d000000000000000042000000" image_path = expanduser("~") + "/Pictures/Screenshots/" while True: win32clipboard.OpenClipboard() if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_DIB): data = win32clipboard.GetClipboardData(win32clipboard.CF_DIB) if not exists(image_path): mkdir(image_path) with open(f"{image_path}{get_timestamp()}.bmp", "wb") as f: f.write(bytearray.fromhex(bmp_header_hex)) f.write(data) win32clipboard.EmptyClipboard() win32clipboard.CloseClipboard() sleep(2)
def paste(self): win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData() win32clipboard.CloseClipboard() setText(self.t1, data) self.wrapper()
def send_to_clipboard(clip_type, data): win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(clip_type, data) win32clipboard.CloseClipboard()
def gettext(): w.OpenClipboard() t = w.GetClipboardData(win32con.CF_TEXT) w.CloseClipboard() return t