Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyboard history program
#1
Hi guys!

I've been making a keyboard history program called ClipboardHero in python which stores your last clipboard entries in a file. I have encountered two problems while making this program:
  • I need the Ctrl-C keys to activate the program even when the window is not in focus
  • I need it to activate not only with Ctrl-C but with right-click+Copy as well

I would grateful if anyone could help! Smile

The code:
from tkinter import *
from tkinter import messagebox
import winsound

subMenu = Tk()
subMenu.title("ClipboardHero")
subMenu.attributes("-topmost", True)
subMenu.overrideredirect(True)
subMenu.withdraw()

menuShown = False

clipboardSaves = open("clipboard.txt", "a")
clipboardSaves.close()

def copy():
    global menuShown
    clipboard = str(subMenu.clipboard_get())

    clipboardSaves = open("clipboard.txt", "a+")
    clipboardSaves.write("\n" + clipboard)
    clipboardSaves.close()

    messagebox.showinfo("ClipboardHero - Clipboard saved", "Your current clipboard has been saved!\n" + clipboard)
    menuShown = False
    subMenu.withdraw()

def hideMenu():
    subMenu.withdraw()
    menuSHown = False
    
def showMenu(event):
    global menuShown
    
    if menuShown == False:
        winsound.Beep(400, 300)
        winsound.Beep(600, 300)
        
        subMenu.deiconify()

        Label(subMenu, text = "Do you want to copy this text to ClipboardHero?").grid(row = 0, column = 0)

        yes = Button(subMenu, text = "Yes", width = 50, bg = "green", command = copy)
        yes.grid(row = 1, column = 0)

        no = Button(subMenu, text = "No", width = 50, bg = "red", command = hideMenu)
        no.grid(row = 2, column = 0)

        menuShown = True
        subMenu.mainloop()

subMenu.bind("<Control-c>", showMenu)
Reply
#2
This is a bit late, but if you still need to capture ctrl-c:
here's the general way to capture ctrl-c within a python script.
  • 1st add import signal at top of script.
  • somewhere near the (logical) start of your program, add:
    signal.signal(signal.SIGINT, self.capture_ctrl_c)
  • Add a function:
    def capture_ctrl_c(self, sig, frame):
        print('User interrupt encountered\nAdd your ctrl-C code here')
        ...
Reply
#3
I know how to capture ctrl-c. It's right-click copy I need too.

The issues above I fixed exceptr the right-click copy
Reply
#4
You will have to create your own event binding for this.
Download John Shipman's tkinter manual here
go to page 158 and read the section 54.2 to learn how to do this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Build a DNS History Rudeus 2 607 Aug-11-2023, 09:52 PM
Last Post: Rudeus
  why is yfinance returning an ellipsis in the middle of ticket history db042190 3 916 Jun-12-2023, 06:03 PM
Last Post: db042190
  Coding a logger for firefox history kpiatrou 2 2,835 Dec-25-2018, 06:42 PM
Last Post: snippsat
  How to handle "History" in ptpython sylas 4 3,936 Oct-07-2018, 11:09 PM
Last Post: Larz60+
  How to differentiate displacement-time history muhsin 1 2,786 Feb-14-2018, 11:50 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020