-1

I made a password generator, and now I need to make it copy the generated password when pressing the key "C", I've tried "input("press c to copy") but it didn't work. any ideas?

import random

print("Welcome to the password generator")

input("press enter to generate a password : ")

def password(length):

    pw = str()

    characters = "abcdefghijklmnopqurstuvwxyz"

    numbers = "123456789"

    weird= "/?!$£*<>"

    for i in range(length):

        pw = pw + random.choice(characters) + random.choice(numbers) + random.choice(weird)

    print(pw)

    return pw


password(4)

#this's what I tried

input("press c to copy") 
  • 1
    Does this answer your question: https://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python – Peter Pesch May 18 '20 at 16:49
  • Does this answer your question? [How do I make python wait for a pressed key?](https://stackoverflow.com/questions/983354/how-do-i-make-python-wait-for-a-pressed-key) – quamrana May 18 '20 at 16:49

4 Answers4

1

install and use the pyperclip library

to install:

pip install pyperclip

simple usage:

import pyperclip

pyperclip.copy(pw)

your code will be like this:

import random
import pyperclip

print("Welcome to the password generator")
input("press enter to generate a password : ")

def password(length):
    pw = str()
    characters = "abcdefghijklmnopqurstuvwxyz"
    numbers = "123456789"
    weird= "/?!$£*<>"

    for i in range(length):
        pw = pw + random.choice(characters) + random.choice(numbers) + random.choice(weird)

    print(pw)

    return pw


pw = password(4)
pyperclip.copy(pw)

The code I provided will copy the password without pressing any key.

Rane
  • 136
  • 2
  • 7
0

If you wanted to copy no matter what you could try:

import pyperclip
pyperclip.copy(password(4))

If you wanted to wait till you hit C there are 2 ways to do this The simple way is to hit c and then hit enter then do an if statement if the input that came was a c like this

import pyperclip
key = input("press c to copy: ") 
if key == "c":
   pyperclip.copy(password(4))

the other way is to use pynput to listen to all your keyboard strokes.

smal
  • 177
  • 10
  • 1
    Note that `pyperclip` is not a part of Python's standard library, so the user needs to `pip install pyperclip` first. – jfaccioni May 18 '20 at 16:59
0

Use the Keyboard Module pip install keyboard

Here is an example of using it(Hope this helps)

import keyboard

while True:

    if(keyboard.is_pressed('c')):
        # Do Stuff

MattMcCann
  • 40
  • 9
0

First, import pyautogui, then add this to the bottom of your script:

pyautogui.typewrite(['up'])
pyautogui.typewrite(['up'])
pyautogui.hotkey('shift','end')
pyautogui.hotkey('ctrl','c')

(This only works if you run the script with the python editor.)

Red
  • 26,798
  • 7
  • 36
  • 58