Jump to content

How to BlockInput using c# (with the nuget package)


JohnFromAustralia
 Share

Recommended Posts

Hello there,

I am looking to prevent mouse and keyboard movement while running a macro (else my macro will easily fail). I use the C# nuget package.

I notice (or cannot find) that there is no BlockInputs method to use with the c# wrappers. Am I blind or has that just not been implemented?

Pending that, I also attempted the following stackoverflow post. To no avail.
https://stackoverflow.com/questions/20841501/blockinput-method-doesnt-work-on-windows-7

Does anyone have any idea how I can prevent mouse and keyboard input while a macro is running?

Thank you kindly for any ideas or help
Best Regards
John

Link to comment
Share on other sites

I would do it in c#, but it's proven difficult....that link looks very promising....will give it a try and hopefully I can get it working...

Would be nice if it was in the wrapper tho :) I would have thought it'd be a pretty common requirement...no matter..

Will report back when I find something that works...

Thank you heaps for your tip
John

Link to comment
Share on other sites

  • 2 weeks later...

Hi again,

For anyone who stumbles accross this....I know realise why my C# code wasn't working to blockinputs....its because you need to run as administrator (elevated previleges)...

Here is some little bit of messy code / test unit that works when running as administrator;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace SystemBetPro.Scrape {

    
    public partial class AllTests {

        //https://stackoverflow.com/questions/20841501/blockinput-method-doesnt-work-on-windows-7
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool BlockInput(bool fBlockIt);

        //Example Usage
        public static void BlockInputForInterval(int spanAsMilliseconds) {
            try {
                BlockInput(true);
                Thread.Sleep(spanAsMilliseconds);
            }
            finally {
                BlockInput(false);
            }
        }

        [TestMethod]
        public void BlockInputsSupressInputsTest() {
            BlockInputForInterval(5000);                            
        }   

    }//endclass
} //endnamespace

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...