❔ help needed
so im making a beat game in visual studio and cs can anyone help me with detecting when a key is pressed? i cant figure it out
38 Replies
ping me if u can help
Visual Studio is an IDE, what exactly are you using to make this game?
Console? Winforms? WPF? Avalonia? Godot? UWP? WinUI? MAUI? Xamarin? Unity? UNO? Stride?
winforms i believe
its visual studio winforms .net 6.0 i think
Handle keyboard input at the Form level - Windows Forms .NET
Learn how to handle keyboard input for your Windows Forms at the form level, before messages reach a control.
alr
private void KeyIsDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
MessageBox.Show("aaaa");
}
}
why wont this work?
Did you attach this event to the form?
I'll take it as a "no"
uh yeah
under public Form1() ?
Idk how Winforms works to be honest, not much interested in archeology
But I'd google "winforms how to attach an event to a form"
normally you set it via the designer, but you can do it via code too
in the constructor, you can do
this.EventName += Handler;
idk what event you want to usebro idk now im so confused 😅
KeyIsPressed
is your handler method
you need to attach it to an eventwhere ever i put the code theres a huge error
Show. I can't very well guess what you wrote or where, can I?
okay i fixed the error bit
you didnt put the event registration anywhere thou
ah ive never made key registration so i got no clue
I wrote this, you replied to me.
so this.KeyIsDown += Handler;?
no,
KeyIsDown
is your handler
i have no idea what event you are trying to useKeyPress
or KeyDown
apparentlyso this.Event += KeyIsDown;?
yes, but
Event
should be the name of the actual event you want to register on
KeyDown
most likelyah
so uh
this after the if function? this.KeyIsDown += Handler;
in the constructor
uhh
public Form1()
thats your constructor.this.KeyIsDown += KeyDown;
you flipped them again.
this.KeyDown+= KeyIsDown;
yep
???
yup
awesome tysm
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.