Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

User Image
Gregory Diebold
49 discussion posts
I am fairly new to C# & am looking for a C# equivalent to Pascal's inkey() which captures keyboard codes during a parameter defined delay.

I have used IsKeyDown(), as in the snippet below, but for my next project I will be in a loop until a specific keypress:

BFS.Audio.SetMute(false);
        BFS.Audio.SetVolume(0.15m);
        BFS.DisplayFusion.RunFunction("Enable Screen Saver");
        
        if (BFS.Input.IsKeyDown("83"))
        {
            BFS.Dialog.ShowMessageInfo("Bootup No Sound");
            BFS.Audio.SetMute(true);
        }
        else
        {
            BFS.Audio.PlayWAV("C:\\Windows\\Media\\Windows Shutdown.wav");
            BFS.General.ThreadWait(500);
            BFS.DisplayFusion.RunFunction("Start Screen Saver");
            BFS.Speech.TextToSpeechWithVoice("Good Morning Joe,   System Ready,, Happy computing", "");
        }


I'll keep researching Stack Overload / Google, but would appreciate real-world experiences, too.

See attachments, comments welcome.
• Attachment: inkey.jpg [504,411 bytes]
inkey.jpg
inkey.jpg
• Attachment: inkey.png [340,912 bytes]
inkey.png
inkey.png
Jan 14, 2024 (modified Jan 14, 2024)  • #1
User Image
JLJTGR
101 discussion posts
You can try using something like this:

Code

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
        bool keyUsed = false;
        while(sw.ElapsedMilliseconds < 500)
        {
            if (BFS.Input.IsKeyDown("17"))
            {
                keyUsed = true;
                break;    // Immediately leave the loop
            }
            //BFS.General.ThreadWait(1);    // 100x takes 3.15s
            System.Threading.Thread.Sleep(1);    // 100x takes 1.55s
        }
        if (keyUsed)
        {
            BFS.Dialog.ShowTrayMessage($"Key was used after {sw.Elapsed.TotalSeconds:0.00}s");
        }
        else
        {
            BFS.Dialog.ShowTrayMessage($"Key was NOT used after {sw.Elapsed.TotalSeconds:0.00}s");
        }
    }
}


There appeared to be a lot of overhead with loops and thread sleep/wait... so I used a stopwatch object to determine actual time to wait.
Jan 14, 2024  • #2
Subscribe to this discussion topic using RSS
Was this helpful?  Login to Vote(1)  Login to Vote(-)