about RegisterHotkey

N

NewBie

Dear All.

I find a class from GotDotNet which can register hotkey.
Then I make some change that let it can use on vb & CF.
Everything is fine. But, the hotkey event "PreFilterMessage()" can`t be
fire.
Please give me a hand and Thanks a lot ~~!!


code in my program
=======================
Friend WithEvents iKey As Durius.Generics.Hotkey
iKey = New Durius.Generics.Hotkey(Me.Handle)
hKeyID = iKey.RegisterHotkey(System.Windows.Forms.Keys.F2,
Durius.Generics.Hotkey.KeyFlags.MOD_NONE)


Class Source
===============

using System;
using OpenNETCF;
using OpenNETCF.Windows.Forms ;

namespace Durius.Generics
{
public delegate void HotkeyEventHandler(int HotKeyID);

public class Hotkey : OpenNETCF.Windows.Forms.IMessageFilter
{
System.Collections.Hashtable keyIDs = new System.Collections.Hashtable();
IntPtr hWnd;

public event HotkeyEventHandler OnHotkey;
public enum KeyFlags
{
MOD_NONE = 0x0,
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
}

[System.Runtime.InteropServices.DllImport("coredll.dll")]
public static extern UInt32 RegisterHotKey( IntPtr hWnd, UInt32 id, UInt32
fsModifiers, UInt32 vk);
[System.Runtime.InteropServices.DllImport("coredll.dll")]
public static extern UInt32 UnregisterHotKey( IntPtr hWnd, UInt32 id);
[System.Runtime.InteropServices.DllImport("coredll.dll")]
public static extern UInt32 GlobalAddAtom( String lpString );
[System.Runtime.InteropServices.DllImport("coredll.dll")]
public static extern UInt32 GlobalDeleteAtom( UInt32 nAtom );


public Hotkey(IntPtr hWnd)
{
this.hWnd = hWnd;
OpenNETCF.Windows.Forms.ApplicationEx.AddMessageFilter(this);
}


public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags
keyflags)
{
uint rKey;
UInt32 hotkeyid =
GlobalAddAtom(OpenNETCF.GuidEx.NewGuid().ToString());//System.Guid.NewGuid()
..ToString());
rKey = RegisterHotKey( (IntPtr)hWnd, hotkeyid, (UInt32)keyflags,
(UInt32)Key);
keyIDs.Add(hotkeyid, hotkeyid);
return (int)hotkeyid;
}


public void UnregisterHotkeys()
{
OpenNETCF.Windows.Forms.ApplicationEx.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs.Values)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
}

public bool PreFilterMessage(ref Microsoft.WindowsCE.Forms.Message m) //
{
if (m.Msg == 0x312) /*WM_HOTKEY*/
{
if(OnHotkey != null)
{
foreach (UInt32 key in keyIDs.Values)
{
if((UInt32)m.WParam == key)
{
OnHotkey((int)m.WParam);
return true;
}
}
}
}
return false;
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top