Jump to content

Keyboard.is_pressed isn't working

Enderg312
if keyboard.is_pressed('enter'):
    enter()

def enter():
    if user.get() == admin and code.get() == password:
        sys.exit()
    else: 
        l3 = Label(root, text = "check login", font=("Courier", 22))
        l3.grid()

Every time I hit enter on the keyboard nothing happens, it doesn't execute the code.

Link to comment
Share on other sites

Link to post
Share on other sites

Try defining the function enter() before the "if keyboard.is_pressed()" command?

 

def enter():
    if user.get() == admin and code.get() == password:
        sys.exit()
    else: 
        l3 = Label(root, text = "check login", font=("Courier", 22))
        l3.grid()
        
if keyboard.is_pressed('enter'):
    enter()

Like this?

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

Since I don't know your code, I made some alterations and the following code works for me:

import keyboard
def enter():
    print("Hey, you just pressed the Enter Key!")
while True:

    if keyboard.is_pressed('enter'):
        enter()
        exit()

And prints the text when I press the Enter key.

Maybe you've got some problem with other parts of the code?

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

from tkinter import *
import subprocess
import sys
import keyboard

root = Tk()
root.title("My Login")
root.geometry("650x650")
root.attributes('-fullscreen', True)

frame = Frame(root)

app = Frame(root)
app.grid

l = Label(root, text = "Login",font="Times 30", padx=5,  pady=5)
l.grid()

l1 = Label(root, text = "Username:",font="Times 30", padx=5,  pady=5)
l1.grid()

l2 = Label(root, text = "Password:", font="Times 30", padx=5,  pady=5)
l2.grid()

user = Entry(root)
user.grid( row= 1, column= 2)
user.configure(font = ("Courier", 44))
code = Entry(root)
code.grid( row= 2, column= 2)
code.configure(font = ("Courier", 44),show="*")

if keyboard.is_pressed('enter'):
    enter()

admin = "admin"  
password = "Password"  

def enter():
    if user.get() == admin and code.get() == password:
        sys.exit()
    else: 
        l3 = Label(root, text = "check login", font=("Courier", 22))
        l3.grid()

b1 = Button(root, text = "Enter", command = enter, font=("Courier", 44))
b1.grid()

root.mainloop()

Here's my code

Link to comment
Share on other sites

Link to post
Share on other sites

from tkinter import *
import subprocess
import sys
import keyboard

root = Tk()
root.title("My Login")
root.geometry("650x650")
root.attributes('-fullscreen', True)

frame = Frame(root)

app = Frame(root)
app.grid

l = Label(root, text = "Login",font="Times 30", padx=5,  pady=5)
l.grid()

l1 = Label(root, text = "Username:",font="Times 30", padx=5,  pady=5)
l1.grid()

l2 = Label(root, text = "Password:", font="Times 30", padx=5,  pady=5)
l2.grid()

user = Entry(root)
user.grid( row= 1, column= 2)
user.configure(font = ("Courier", 44))
code = Entry(root)
code.grid( row= 2, column= 2)
code.configure(font = ("Courier", 44),show="*")



admin = "admin"  
password = "Password"  

def enter(event):
    if user.get() == admin and code.get() == password:
        sys.exit()
    else: 
        l3 = Label(root, text = "check login", font=("Courier", 22))
        l3.grid()

b1 = Button(root, text = "Enter", command = enter, font=("Courier", 44))
b1.grid()
root.bind('<Return>', enter)
root.mainloop()

Try this.

I have removed the keyboard.is_pressed() function, and instead, added root.bind(), which is used to detect key presses in Tkinter.

 

And just so you know, you CANNOT call a function before it is defined.

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×