Search code examples
vb.netvisual-studio-2017

"BlockInput" Function is Not Working in Program


I am creating a program that will click the screen wherever the user asks it to. But I want to disable mouse movement/clicking while the program performs the click, otherwise if the user is moving the mouse when the program performs the click it could drag the mouse away from where the mouse was supposed to click.

Currently I've tried using:

Private Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Boolean) As Boolean

&

Private Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Integer) As Integer

And then calling either BlockInput(True/False) or BlockInput(1/0) depending on how I declare the `BlockInput' function.

Currently neither stops the mouse from moving when I drag it across the screen.

Is there something I need to include in my code to allow this function to work? I'm aware that disabling the mouse this way might also disable the program itself from performing the click as well but I haven't even been able to test that since I can't get that far.

Any help is appreciated, thank you!


Solution

  • tested and made me reboot :) Careful. Working on win 10

    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
    <DllImport("user32.dll", EntryPoint:="BlockInput")> _
    Private Shared Function BlockInput(<MarshalAs(UnmanagedType.Bool)> ByVal fBlockIt As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        BlockInput(True)
    End Sub
    
    End Class