简体   繁体   中英

How to make ursina detect the shift key in python?

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.

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')

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

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM