2

I'm trying to block input in my program but it doesn't work ...I read about this article and didn't find solution only the problem from vista-7 and above windows... also I found unsolved topic here and I wish from you to help me...

the code from

"mr. LeftTechticle" at youtube video

and he ran it perfectly...

I tried the code but nothing happened...

//------------------------------------------Block Class----------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace WindowsFormsApplication14
    {
        static class InputBlocker
        {
            [DllImport("user32.dll")]
            static extern bool BlockInput(bool fBlockIt);
            private static Timer timer = new Timer();
            static InputBlocker()
            {
                timer.Tick += new EventHandler(tick);
            }
            public static void Block(int mill)
            {
                BlockInput(true);
                timer.Interval = mill;
                timer.Start();
            }
            private static void tick(object sender ,EventArgs e)
            {
                BlockInput(false);
                timer.Stop();
            }
        }
    }

//------------------------------------------Form class---------------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication14
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                InputBlocker.Block(10000);
            }
        }
    }
Radi Soufan
  • 89
  • 1
  • 8
  • possible duplicate of [How can I block keyboard and mouse input in C#?](http://stackoverflow.com/questions/586547/how-can-i-block-keyboard-and-mouse-input-in-c) – Vitor Canova Dec 30 '13 at 14:46
  • Dear Vitor as you see I'm putting the code the problem is not with the code the problem with windows 7 System doesn't work with those statements.. any ideas? – Radi Soufan Dec 30 '13 at 14:52
  • Just point out that they are talking about similar problem there. They even have a code sample that should do what you want but someone point out it didn't work on their Windows 7. Have you tried get the return of `BlockInput(true)` to see it´s `true` or `false`? – Vitor Canova Dec 30 '13 at 15:00
  • hi ..I tested it it was false.. O.o why is that? – Radi Soufan Dec 30 '13 at 15:28

1 Answers1

4

Properly implementing pinvoke is 95% of the battle. It needs to look like this:

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool BlockInput(bool fBlockIt);

    public static void Block(int mill)
    {
        if (!BlockInput(true)) {
            throw new System.ComponentModel.Win32Exception();
        }
        // etc..
    }

With the expectation that you now get a good exception message that tells you why it failed. With the expectation that you'll get "Access is denied", preventing the user from operating his machine requires UAC elevation, for obvious reasons.

It is an awfully big hammer, implementing IMessageFilter and swallowing all input events is the lesser evil.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • so Mr. Hans can I get from you that it's not possible to use this way for blocking movement..? if yes can you tell me how can I black the input actions because all that is just an understanding for the method Ill use to create my function that block input from user until he inter the right password while the windows is running without locking the winows – Radi Soufan Dec 30 '13 at 15:39
  • oh ok ok I got it Ill try>> – Radi Soufan Dec 30 '13 at 15:46
  • 1
    Hmm, I gave you a link that showed you how to make it possible. Fairly ironic that the UAC elevation dialog will also ask for a password, isn't it? It is just the completely wrong approach, BlockInput() doesn't actually stop a user from regaining control of the machine. Ctrl+Alt+Del and Task Manager gives it back. The *secure* password entry dialog that Windows uses switches the desktop. – Hans Passant Dec 30 '13 at 15:47
  • @RadiSoufan Consider maks Hans answer as "the answer" of this question. – Vitor Canova Dec 30 '13 at 17:31
  • ya, sorry about my lack of rules knowledge and I want to thank Mr. Han and Mr. Vitor :) – Radi Soufan Dec 30 '13 at 19:29