1

I want to Disable Alt+Esc,Alt+Tab,Win key,Ctrl+Esc key In my project. After A lot Of search I found This Source code.It Working Fine. But some Times It gives An error.

"CallbackOnCollectedDelegate was detected Message: A callback was made on a garbage collected delegate of type 'over_ride_sort_keys!over_ride_sort_keys.Form1+LowLevelKeyboardProcDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called."

  • How to over come this?
  • My Source code is given below.
  • Also Tell me How to disable Alt + f4?

Source code Which i have used is given below

Code

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 Microsoft.VisualBasic;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace over_ride_sort_keys
{
    public partial class Form1 : Form
    {
        [DllImport("user32", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

        public static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);
        [DllImport("user32", EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool UnhookWindowsHookEx(IntPtr hHook);
        [DllImport("user32", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int CallNextHookEx(IntPtr hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        public delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        const int WH_KEYBOARD_LL = 13;

        public struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        int TECLEADO;
        public void COLORES()
        {
            ButtonALTTAB.BackColor = Color.Black;
            ButtonALTESC.BackColor = Color.Black;
            ButtonCTRLESC.BackColor = Color.Black;
            ButtonWIN.BackColor = Color.Black;
            ButtonTODAS.BackColor = Color.Black;
        }

        private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {

            bool ALTTAB = ((lParam.vkCode == 9) && (lParam.flags == 32));
            bool ALTESC = ((lParam.vkCode == 27) && (lParam.flags == 32));
            bool CTRLESC = ((lParam.vkCode == 27) && (lParam.flags == 0));
            bool WIN = ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1));
            bool TODAS = ((lParam.vkCode == 9) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 0)) | ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1));

            if (ALTTAB == true & ButtonALTTAB.BackColor == Color.Red)
            {
                return 1;
            }
            else if (ALTESC == true & ButtonALTESC.BackColor == Color.Red)
            {
                return 1;
            }
            else if (CTRLESC == true & ButtonCTRLESC.BackColor == Color.Red)
            {
                return 1;
            }
            else if (WIN == true & ButtonWIN.BackColor == Color.Red)
            {
                return 1;
            }
            else if (TODAS == true & ButtonTODAS.BackColor == Color.Red)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(IntPtr.Zero, nCode, wParam, ref lParam);
            }
        }
        private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            FormClosing += Form1_FormClosing;
        }

        private void ButtonALTTAB_Click(object sender, EventArgs e)
        {
            if (ButtonALTTAB.BackColor == Color.Black)
            {
                COLORES();
                ButtonALTTAB.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonALTTAB.BackColor = Color.Black;
            }
        }

        private void ButtonALTESC_Click(object sender, EventArgs e)
        {
            if (ButtonALTESC.BackColor == Color.Black)
            {
                COLORES();
                ButtonALTESC.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonALTESC.BackColor = Color.Black;
            }
        }

        private void ButtonCTRLESC_Click(object sender, EventArgs e)
        {
            if (ButtonCTRLESC.BackColor == Color.Black)
            {
                COLORES();
                ButtonCTRLESC.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonCTRLESC.BackColor = Color.Black;
            }
        }

        private void ButtonWIN_Click(object sender, EventArgs e)
        {
            if (ButtonWIN.BackColor == Color.Black)
            {
                COLORES();
                ButtonWIN.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonWIN.BackColor = Color.Black;
            }
        }

        private void ButtonTODAS_Click(object sender, EventArgs e)
        {
            if (ButtonTODAS.BackColor == Color.Black)
            {
                COLORES();
                ButtonTODAS.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonTODAS.BackColor = Color.Black;
            }
        }
    }
}
RAFEEK CJ
  • 41
  • 7
  • I'm not a C# programmer, but isn't `&` a bitwise-and instead of the logical-and (probably `&&`) that you'd need in these `if` statements? – Robert Sep 04 '16 at 23:41
  • Did you even bother to google the error message? https://stackoverflow.com/questions/4855513/callbackoncollecteddelegate-was-detected and https://stackoverflow.com/questions/7428764/callbackoncollecteddelegate-was-detected – Robert Sep 04 '16 at 23:45
  • Possible duplicate of [CallbackOnCollectedDelegate in globalKeyboardHook was detected](http://stackoverflow.com/questions/9957544/callbackoncollecteddelegate-in-globalkeyboardhook-was-detected) – Robert Sep 04 '16 at 23:45
  • those if statements are not required ...that's not the problem here... – RAFEEK CJ Sep 05 '16 at 05:05

0 Answers0