Skip to content

Instantly share code, notes, and snippets.

@lukifer195
Created July 30, 2020 07:23
Show Gist options
  • Save lukifer195/245671b4ef7c7f93ece6669ea89e9bfe to your computer and use it in GitHub Desktop.
Save lukifer195/245671b4ef7c7f93ece6669ea89e9bfe to your computer and use it in GitHub Desktop.
Get text clipboard
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