简体   繁体   中英

How to have an event While Holding a key in c#

I want to create an audio recording application that records your voice while your pressing "X" on your keyboard. Or something like "Hold X to record your voice" I just want to know how to to achieve that, I already know to to record a voice, just the Key pressing only. This is in Windows Form Application. Please help im a beginner.

At its simplest, you can have event handlers for the KeyDown and KeyUp events, and do something in those event handlers. The events can be handled by the window (form) itself but will depend on what control has the focus; if the cursor is in a textbox the window itself will not raise those events anymore, so you need to set the KeyPreview property of the form to true for the form to intercept them.

Ok you most have object to do that like textbox or something and then add two lambda to OBJ one for recording a other for Dis-recording To form when it load like this:

private void form1_Load(object sender, EventArgs e)
    {
        obj.KeyDown += new KeyEventHandler(delegate (object o, KeyEventArgs a)
        {
            //Record Voice
        });
        obj.KeyUp += new KeyEventHandler(delegate (object o, KeyEventArgs a)
        {
            //Stop Recording
        });

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