I have this program that I found on the internet somewhere. I am trying to add to it and need a little help.

I am a vb.net man and I am trying to do this in C#.

What this little program does it hide the taskbar, star menu, ctrl+esc and a couple other keyboardhooks.....

When I click the "hide" button the taskbar and start menu hide along with disabling the keys. When I click the "show" button the taskbar and start menu reappear, however, the keys are still disabled.

I would like them enabled upon clicking "show"

I have attached a zip file of the program. I am adding to it so there is another form that dosn't do anything.

Could someone get me on the right track and show me how to enable the keys when the "show" button is clicked?

Thank you

daveofgv

daveofgv,
Suggestions:
1. C# is case sensitive.
2. Read MSDN Pages - Win32 API

Thank you for your reply. I do know C# is case sensative (was there something wrong in my code so far?)

I checked out msdn, however, there are so many don't know which one to look at.

Just wondering how to enable them


daveofgv

Maybe I should ask this in a different way..........

Quick question.

I am using:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class KeyboardFilter
{
    private Keys[] mFilter;
    private IntPtr mHook;
    private LowLevelKeyboardProc mProc;

    public KeyboardFilter(Keys[] keysToFilter)
    {
        // Install hook
        mFilter = keysToFilter;
        ProcessModule mod = Process.GetCurrentProcess().MainModule;
        mProc = new LowLevelKeyboardProc(KeyboardProc);   // Avoid garbage collector problems
        mHook = SetWindowsHookEx(13, mProc, GetModuleHandle(mod.ModuleName), 0);
        if (mHook == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to set hook");
    }
    public void Dispose()
    {
        // Release hook
        if (mHook != IntPtr.Zero)
        {
            UnhookWindowsHookEx(mHook);
            mHook = IntPtr.Zero;
        }
    }
    private IntPtr KeyboardProc(int nCode, IntPtr wp, IntPtr lp)
    {
        // Callback, filter key
        if (nCode >= 0)
        {
            KBDLLHOOKSTRUCT info = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
            foreach (Keys key in mFilter)
                if ((key & Keys.KeyCode) == info.key && CheckModifier(key)) return (IntPtr)1;
        }
        return CallNextHookEx(mHook, nCode, wp, lp);
    }
    private bool CheckModifier(Keys key)
    {
        // Check if modifier key in required state
        if ((key & Keys.Control) == Keys.Control &&
          GetAsyncKeyState(Keys.LControlKey) == 0 && GetAsyncKeyState(Keys.RControlKey) == 0) return false;
        if ((key & Keys.Shift) == Keys.Shift &&
          GetAsyncKeyState(Keys.LShiftKey) == 0 && GetAsyncKeyState(Keys.RShiftKey) == 0) return false;
        if ((key & Keys.Alt) == Keys.Alt &&
          GetAsyncKeyState(Keys.LMenu) == 0 && GetAsyncKeyState(Keys.RMenu) == 0) return false;
        return true;
    }

    // P/Invoke declarations
    [StructLayout(LayoutKind.Sequential)]
    private struct KBDLLHOOKSTRUCT
    {
        public Keys key;
        public int scanCode;
        public int flags;
        public int time;
        public IntPtr extra;
    }
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool UnhookWindowsHookEx(IntPtr hook);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string name);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern short GetAsyncKeyState(Keys key);
}

in my class file and:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TaskbarHide
{
    public partial class main : Form
    {
        public main()
        {
            InitializeComponent();
        }

        private void cmdHide_Click(object sender, EventArgs e)
        {
            Taskbar.Hide();
            KeyboardFilter filter = new KeyboardFilter(new Keys[] { Keys.LWin, Keys.RWin, Keys.Escape | Keys.Control });
        }

        private void cmdShow_Click(object sender, EventArgs e)
        {
            Taskbar.Show();
            
        }

        private void main_Load(object sender, EventArgs e)
        {
             
        }

        private void cmdHide_KeyDown(object sender, KeyEventArgs e)
        {
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();

        }
    }
}

in my main form file.....

My question is does anyone know how to unhook my KeyboardFilter?

I would like to unhook these keys in the:

private void cmdShow_Click(object sender, EventArgs e)
        {
            Taskbar.Show();
            
        }

but I can't get it right....

Can anyone help me?

Thank you in advanced

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.