0

i have a windows form which looks like a widget

i have to disable combination keys like alt+f4 win+d . because widget have no properties of minimize close with shortcut keys.

i have disabled the win+d keys in my widget but it disabled the shorcut for all the apps

what i have to do, is to only disable shorcut keys for my application like widget .

when some1 press any win+d then the background application like internet explorer(if open) will be minimized or maximized.

any idea how can i do that ?

like rain meter or like xwidget have the same properties! any idea how can they do that?

user1693655
  • 1
  • 1
  • 4
  • Why would you want to disable Win+X and Win+D? I understand why for Alt+F4, but I don't see the need to disable the two for a "widget". – Alvin Wong Feb 23 '13 at 07:25
  • @AlvinWong win+x is example , and for win+d , i want my win form same as widget and i m getting closer to it, i dont want to create widget project because , my widget will have to work on different os and specifications, – user1693655 Feb 23 '13 at 07:31
  • I believe you're making a simple problem complicated. You could explain the behaviour that you expect in your program, because I think that you might have thought the wrong way. – Alvin Wong Feb 23 '13 at 07:34
  • simply i want that. i dont want my forms to be minimized but the application(if open like music player) will be minimized or maximized – user1693655 Feb 23 '13 at 07:51
  • So the main point is **to prevent your form from being minimized**, but *not* disabling those key combinations. – Alvin Wong Feb 23 '13 at 07:56
  • Then I believe you should rewrite your question to reflect that. – Alvin Wong Feb 23 '13 at 07:58

3 Answers3

0

You can't do that in Winforms

You must create a Widget Project

Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44
  • While a "widget project" may solve the problem, you shouldn't say that it absolutely can't be done in WinForms. – Alvin Wong Feb 23 '13 at 07:18
  • then, may i see your answers `:)` – spajce Feb 23 '13 at 07:20
  • 2
    Technically, it can be done, but for practical purposes, saying it can't be done is close enough. It shouldn't be done, at any rate. –  Feb 23 '13 at 07:25
  • i agree @hvd why we need do that in winform, we could but we need some tricky codes or another parties. – spajce Feb 23 '13 at 07:28
  • widget have no properties of minimize thats why i want to do that. – user1693655 Feb 23 '13 at 07:29
  • i m done with disable the keys , but it disabled for all applications , when i run my app then all shorcuts are disable but i want it for my application not for all. through hook i do that. – user1693655 Feb 23 '13 at 07:30
  • you can simply enable the key disable only when your form is active – Benny Feb 23 '13 at 07:33
  • 1
    @user1693655 I think you're misunderstanding this answer. You're not creating a widget. You're creating a form, and then looking at how it differs from a widget, and change all of those things one at a time. You shouldn't even be trying that. Why not just create a widget if you want a widget? Then you won't have to disable any global shortcut keys, because the global shortcut keys will be doing the right thing already. –  Feb 23 '13 at 07:35
  • @hvd mine forms will run in any os version. thatz why im doing that. there are several application which runs in background and if some1 press any shortcut key then it will not be minimized – user1693655 Feb 23 '13 at 07:46
  • @user1693655 Presumably, by "any os version", you mean "any Windows version, as long as it is at least Windows XP"? Even so, they won't be using keyboard hooks for that. What if the user clicks the "Show Desktop" shortcut in the Quick Launch bar on an XP system? –  Feb 23 '13 at 07:56
  • @hvd good point , i did not thought that , but i want to make it for all support for all o.s windows version. thats why i m asking how can i do that? – user1693655 Feb 23 '13 at 08:08
  • 1
    @user1693655 And I don't know, I just know that answers to the question you've asked are not going to help you. Disabling Win+D is the wrong thing to do, and if you leave it enabled when other applications are active, it will *also* minimise your window. Figure out the right question to ask, and you'll get answers that are much more useful. I *think* the right question to ask is "how do I prevent my window from being minimised", but I do not know for sure if that does not miss some other details. –  Feb 23 '13 at 08:17
0

You can use global keyboard hook, you can see an example of how to do it here, Then just listen to win + d combination in your hook and in this case don't pass the call forward

Benny
  • 207
  • 1
  • 7
0
public partial class MainForm : Form
{
        readonly KeyboardFilter kbFilter = new KeyboardFilter(new Keys[] 
        { 
            Keys.LWin | Keys.D,
            Keys.RWin | Keys.D, 
            Keys.LWin | Keys.X, 
            Keys.RWin | Keys.X,
            Keys.Alt | Keys.F4
        });
}

Be sure to use KeyboardFilter.Dispose() in the Form_Closing event.

KeyboardFilter class was taken from Here

class KeyboardFilter : IDisposable
{
    private Keys[] mFilter;
    private IntPtr mHook;
    private readonly LowLevelKeyboardProc mProc;

    public KeyboardFilter(Keys[] keysToFilter)
    {
        // Install hook
        mFilter = keysToFilter;
        ProcessModule mod = Process.GetCurrentProcess().MainModule;
        mProc = 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()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // Release hook
            if (mHook != IntPtr.Zero)
            {
                UnhookWindowsHookEx(mHook);
                mHook = IntPtr.Zero;
            }
        }
    }
    ~KeyboardFilter()
    {
        Dispose(false);
    }
    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 static 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); 
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • Ok, you say that "you found this somewhere on the Internet", then you should also reference the source and give credit to it. – Alvin Wong Feb 23 '13 at 07:29
  • @AppDeveloper it will disable win keys for my form and for all it will be enebled? like running my form and sm1 press win+d then it should not minimized my app but minimize the backgroud application like i.e or music playeer? – user1693655 Feb 23 '13 at 07:40
  • @AppDeveloper it works global. will be done at all applications! and i dont want to disable it for all – user1693655 Feb 23 '13 at 07:53
  • well, extend the KeyBoardFilter, add a boolean property, supress key only when form is Normal | Maximized! – Parimal Raj Feb 23 '13 at 07:58
  • can you provide some code because the above code will ragister the key and disable it @AppDeveloper – user1693655 Feb 23 '13 at 08:05
  • it will just register them not disable it, disable is done in : `KeyboardProc` to ignore a Hook change condition to `if (!isMinimized && (key & Keys.KeyCode) == info.key && CheckModifier(key)) return (IntPtr)1;` – Parimal Raj Feb 23 '13 at 08:18
  • @AppDeveloper the code is doing the wrong thing , as i said my forms will not minimixed but all the other apps will minimized but that is not happening!! – user1693655 Feb 25 '13 at 04:52