Jump to content
JohnFromAustralia

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

Recommended Posts

JohnFromAustralia

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

Share this post


Link to post
Share on other sites
JohnFromAustralia

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

Share this post


Link to post
Share on other sites
JohnFromAustralia

Just an update...I've had no success with that...looked promising, but nothing seems to get supressed...talk about rabbit warrens....wish I had an answer to supply....will return with one when I get there...

Share this post


Link to post
Share on other sites
JohnFromAustralia

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

 

  • Like 1

Share this post


Link to post
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

×