Created
July 30, 2020 07:23
-
-
Save lukifer195/245671b4ef7c7f93ece6669ea89e9bfe to your computer and use it in GitHub Desktop.
Get text clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
CF_TEXT = 1 | |
kernel32 = ctypes.windll.kernel32 | |
kernel32.GlobalLock.argtypes = [ctypes.c_void_p] | |
kernel32.GlobalLock.restype = ctypes.c_void_p | |
kernel32.GlobalUnlock.argtypes = [ctypes.c_void_p] | |
user32 = ctypes.windll.user32 | |
user32.GetClipboardData.restype = ctypes.c_void_p | |
def get_clipboard_text(): | |
user32.OpenClipboard(0) | |
try: | |
if user32.IsClipboardFormatAvailable(CF_TEXT): | |
data = user32.GetClipboardData(CF_TEXT) | |
data_locked = kernel32.GlobalLock(data) | |
text = ctypes.c_char_p(data_locked) | |
value = text.value | |
kernel32.GlobalUnlock(data_locked) | |
return value | |
finally: | |
user32.CloseClipboard() | |
print(get_clipboard_text()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment