C
C#2y ago
infirmn

❔ 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
infirmn
infirmnOP2y ago
ping me if u can help
Angius
Angius2y ago
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?
infirmn
infirmnOP2y ago
winforms i believe its visual studio winforms .net 6.0 i think
infirmn
infirmnOP2y ago
alr private void KeyIsDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Space) { MessageBox.Show("aaaa"); } } why wont this work?
Angius
Angius2y ago
Did you attach this event to the form?
infirmn
infirmnOP2y ago
Angius
Angius2y ago
I'll take it as a "no"
infirmn
infirmnOP2y ago
uh yeah under public Form1() ?
Angius
Angius2y ago
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"
Pobiega
Pobiega2y ago
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 use
infirmn
infirmnOP2y ago
bro idk now im so confused 😅
Pobiega
Pobiega2y ago
KeyIsPressed is your handler method you need to attach it to an event
infirmn
infirmnOP2y ago
where ever i put the code theres a huge error
Pobiega
Pobiega2y ago
Show. I can't very well guess what you wrote or where, can I?
infirmn
infirmnOP2y ago
okay i fixed the error bit
Pobiega
Pobiega2y ago
you didnt put the event registration anywhere thou
infirmn
infirmnOP2y ago
ah ive never made key registration so i got no clue
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
I wrote this, you replied to me.
infirmn
infirmnOP2y ago
so this.KeyIsDown += Handler;?
Pobiega
Pobiega2y ago
no, KeyIsDown is your handler i have no idea what event you are trying to use
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
KeyPress or KeyDown apparently
infirmn
infirmnOP2y ago
so this.Event += KeyIsDown;?
Pobiega
Pobiega2y ago
yes, but Event should be the name of the actual event you want to register on KeyDown most likely
infirmn
infirmnOP2y ago
ah so uh this after the if function? this.KeyIsDown += Handler;
Pobiega
Pobiega2y ago
in the constructor
infirmn
infirmnOP2y ago
uhh
Pobiega
Pobiega2y ago
public Form1() thats your constructor.
infirmn
infirmnOP2y ago
this.KeyIsDown += KeyDown;
Pobiega
Pobiega2y ago
you flipped them again.
infirmn
infirmnOP2y ago
this.KeyDown+= KeyIsDown;
Pobiega
Pobiega2y ago
yep
infirmn
infirmnOP2y ago
???
Pobiega
Pobiega2y ago
yup
infirmn
infirmnOP2y ago
awesome tysm
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server