I need to block the use of the keyboard and mouse in form while it will execute a certain function of the system, in which I am putting the call of the lock method in the event of Load
of the form, in which it is actually blocking the keyboard and the mouse, but does not execute the method that is just after your call. Here is a test code I'm doing to test this:
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;
using System.Runtime.InteropServices;
namespace GSD
{
public partial class FormTeste : Form
{
public FormTeste()
{
InitializeComponent();
}
private void FormTeste_Load(object sender, EventArgs e)
{
SetHookMouse();
SetHookTeclado();
Teste();
}
private void Teste()
{
//Implementação do método
}
//------------------------------------------------------------------------------------------------------------------------------------
//
//Raliza o Bloqueio do Mouse
//
//-------------------------------------------------------------------
// Importa as funções que serão usadas
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookExMouse(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookExMouse(IntPtr hInstance);
[DllImport("user32.dll")]
static extern IntPtr CallNextHookExMouse(IntPtr idHook, int code, int wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibraryMouse(string lpFileName);
private delegate IntPtr LowLevelMouseProc(int code, IntPtr wParam, IntPtr lParam);
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
const int WH_MOUSE_LL = 14; // Tipo de hook que será usado
private LowLevelMouseProc hookMouse = hookProcMouse;
private static IntPtr hhookMouse = IntPtr.Zero;
public void SetHookMouse()
{
IntPtr hInstance = LoadLibraryMouse("User32");
hhookMouse = SetWindowsHookExMouse(WH_MOUSE_LL, hookMouse, hInstance, 0); // Instala o hook para a interceptação dos eventos do mouse
}
public static void UnHookMouse()
{
UnhookWindowsHookExMouse(hhookMouse); // Remove o hook instalado
}
public static IntPtr hookProcMouse(int code, IntPtr wParam, IntPtr lParam)
{
// Se a mensagem recebida for > 0 e o clique do mouse for do botão esquerdo ou direito
if (code >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam || MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam)
{
return (IntPtr)1; // Inibe o clique
}
else
return CallNextHookExMouse(hhookMouse, code, (int)wParam, lParam); // Passa para o próximo evento
}
//------------------------------------------------------------------------------------------------------------------------------------
//
//Raliza o Bloqueio do Teclado
//
//-------------------------------------------------------------------
// Importa as funções que serão usadas
[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookExTeclado(int idHook, LowLevelKeyboardProc callback, IntPtr hInstance, uint threadId);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookExTeclado(IntPtr hInstance);
[DllImport("user32.dll")]
static extern IntPtr CallNextHookExTeclado(IntPtr idHook, int code, int wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibraryTeclado(string lpFileName);
private delegate IntPtr LowLevelKeyboardProc(int code, IntPtr wParam, IntPtr lParam);
const int WH_KEYBOARD_LL = 13; // Tipo de hook que será usado
const int WM_KEYDOWN = 0x100; // Messagem usada para quando uma tecla for pressionada
private LowLevelKeyboardProc hookTeclado = hookProcTeclado;
private static IntPtr hhookTeclado = IntPtr.Zero;
public void SetHookTeclado()
{
IntPtr hInstance = LoadLibraryTeclado("User32");
hhookTeclado = SetWindowsHookExTeclado(WH_KEYBOARD_LL, hookTeclado, hInstance, 0); // Instala o hook para o teclado
}
public static void UnHookTeclado()
{
UnhookWindowsHookExTeclado(hhookTeclado);
}
public static IntPtr hookProcTeclado(int code, IntPtr wParam, IntPtr lParam)
{
if (code >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{ // Quando uma tecla for pressionada
return (IntPtr)1; // Inibe o funcionamento
}
else
return CallNextHookExTeclado(hhookTeclado, code, (int)wParam, lParam); // Passa para o próximo evento
}
private void FormTeste_FormClosing(object sender, FormClosingEventArgs e)
{
UnHookMouse();
UnHookTeclado();
}
}
}
I have tried to use this code as an alternative but it does not really block actually blocking the events generated by the mouse, then with a while the application starts to appear the message that the system has stopped responding. Remembering that all of these code is based on the examples that are available in: Lock keyboard and mouse or prevent user leaving window in C #
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern void BlockInput([In, MarshalAs(UnmanagedType.Bool)]bool fBlockIt);
private void FormTeste_Load(object sender, EventArgs e)
{
BlockInput(true);
Teste();
}