access keycodes by pressing a button

I made Ui for controlling my Player, all is fine , but i have methods that work when i press F on my keyboard , how can I make my button equals to keycode.F ? like when I press the button it types F ? please help :frowning: , i don’t want it in If statement “If(input.getkeydown(keycode.F)” i don’t want this , i tried to make input.getkeydown(keycode.F) and that doesn’t work i couldn’t access the F keyCode.

Change the condition in the if statement to:

bool allowObjMovement = false;

void Update()
{
    if(input.getkey(keycode.F))
    allowObjMovement = true; 
}


Void OnTrigger2D(Colllider2D other)
{ 
       if player.collider...etc && allowObjMovement == true;
       { 
             do something. 
       } 
}

Now for the UI Button try this or set allowObjMovement to true however you want.

Personally, I think you’d be better off using the Input system. If you define an Input called “Push” and set that input to KeyPress F, you can also assign it to a gamepad button, or Mouse Button 3. The input system works better than specifically defining a key, and it allows the user to remap it in case they don’t want to use “F”. Not that the other answers are incorrect, or even less correct. :smiley:

Just my two cents.

@hexagonius it’s not a method , when ever my player collides with something and press F i can move the thing the player colides with around .something like :

Void OnTrigger2D(Colllider2D other){
if player.collider…etc && input.getkeydown(keycode.F){
do something.
}
}

For this you have to create a public method, that you call when the press the F and when u press a UI button.

To call the method when clicking the button, simply go to the inspector and add the method from the On Click() list.