0

how do you make ursina detect when the left and the right shift key is pressed in python.

if not is there any good external library to do so.

Michael
  • 1
  • 1

4 Answers4

1

Here you have all the keys handled by ursina with the values associated. https://github.com/pokepetter/ursina/blob/master/ursina/input_handler.py

So for the right shift key it's :

# On the lib
right_shift = 'right shift'
right_shift_up = 'right shift up'
right_shift_down = 'right shift down'
# For your code
def input(key):
    if key == 'right shift down':
        print('pressed right shift button')

Julien Sorin
  • 703
  • 2
  • 12
1

You have to do this:

from ursina import * #importing everything from ursina

app = Ursina() #Creating window

def input(key): # checking for a key input
    if key == 'shift': # checking particular key
        print('pressed right shift button') # printing if shift key pressed

app.run() # launching the window
Tanay
  • 561
  • 1
  • 3
  • 16
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 02 '22 at 00:24
0

I will provide code for a class and for input function

For the input function:


    def update():

        if held_keys['shift']:
            player.speed=10
            print("Shift is Hold")
            

    def input(key):

        if key == 'shift':
            print("Shift Pressed")
                     
                
                
        if key == 'w' or key == 'a' or key == 'd' or key == 's':
            player.speed=5

For the class:


from ursina import *


app = Ursina()


class Class(Button):
    def __init__(self):
        super().__init__(
            enabled = True
            visible = False
        )
        print("Class Called")

    def input(self, key):
        def key_handler():
            print("Shift Key Pressed")

        if key == 'p':
            key_handler()

        print("Input detected")

Class()


app.run()

Understanding

Ursina Engine offers many possibilities for programmers. One such offering is the ability to collect user input and perform some operation.

You can perform a collection of inputs by using the def input(key): function. You can use the above mentioned function inside or outside a class as shown in the examples above.

But in a class, you have to call the Button object and deactivate it in super.__init__(), so that it doesn't appear on the screen and only then use def __input(self, key):.

Webgraphy

https://www.ursinaengine.org/cheat_sheet.html#Keys
https://www.ursinaengine.org/entity_basics.html#Input
https://www.ursinaengine.org/entity_basics.html#Update
Lixt
  • 201
  • 4
  • 19
0

Here's a quick way to find Ursina's exact syntax for any key pressed :)

from ursina import *

app = Ursina()

class Class(Button):
    def __init__(self):
        super().__init__()
    def input(self, key):
        print ("Input detected >>",str(key))

Class()

app.run()

Or this one.

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()

printKey = ''
you = 0

class Class(Button):
    def __init__(self,position = (0,0,0)):
        super().__init__()
    def input(self, key):
        global info
        global you
        if you == 0:
            you = 1        
            Text.size = 0.1
            Text.default_resolution = 1080 * Text.size
            info = Text(text=str(key),color = color.red)
            info.x = -0.426
            info.y = 0.1
            info.z = 0.0
            info.background = True
            info.visible = True
        else:
            if you == 1:
                you = 0
                destroy(info)

        print ("Input detected >>",str(key))              

Class()

app.run()
Mike
  • 1
  • 1