如何禁用Motorola MC75上的发送和结束键

时间:2011-03-14 07:41:51

标签: c# windows-mobile motorola

如何禁用Motorola MC75上的发送和结束键?

我需要这个

的任何C#示例代码

提前致谢

3 个答案:

答案 0 :(得分:4)

答案 1 :(得分:1)

使用Motorola AppCenter限制正在运行的应用程序。它允许您阻止密钥,程序等。

答案 2 :(得分:0)

编辑:我之前不知道PaulH发布的“AllKeys”解决方案,这应该是比我发布的解决方案更好的解决方案。

我假设您要处理绿色和红色硬件密钥?通话和挂断键?

如果是这种情况,您可以监控关键事件,如果符合您的条件,则选择不将其传递给Windows。

private const int WH_KEYBOARD_LL = 20;
private static int _hookHandle;
private HookProc _hookDelegate;

[DllImport("coredll.dll")]
private static extern int SetWindowsHookEx(int type, HookProc hookProc, IntPtr       hInstance, int m);

[DllImport("coredll.dll")]
private static extern IntPtr GetModuleHandle(string mod);

[DllImport("coredll.dll", SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);

[DllImport("coredll.dll")]
private static extern int CallNextHookEx(HookProc hhk, int nCode, IntPtr wParam, IntPtr lParam);


private bool HookKeyboardEvent(bool action)
{
try
{
    if (action)
    {
        HookKeyboardEvent(false);

        _hookDelegate = new HookProc(HookProcedure);
        _hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, _hookDelegate, GetModuleHandle(null), 0);

        if (_hookHandle == 0)
        {
            return false;
        }
        return true;
    }
    if (_hookHandle != 0)
    {
        //Unhook the previouse one
        UnhookWindowsHookEx(_hookHandle);
        return true;
    }
    return false;
}
catch (Exception ex)
{
    string dump = ex.Message;
    return false;
}
}

private int HookProcedure(int code, IntPtr wParam, IntPtr lParam)
{
try
{
    var hookStruct = (KBDLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof (KBDLLHOOKSTRUCT));
    if (DoHardwareKeyPress(hookStruct.vkCode, hookStruct.scanCode, wParam.ToInt32()))
        return CallNextHookEx(_hookDelegate, code, wParam, lParam);
    else
        return -1;
}
catch (Exception ex)
{
    string dump = ex.Message;
    return -1;
}
}

private bool DoHardwareKeyPress(int softKey, int hardKey, int keyState)
{
try
{
    string keyPressInformation = string.Format("SoftKey = {0}, HardKey = {1}, KeyState = {2}", softKey, hardKey,
                                               keyState);
    if (softKey == 114 && hardKey == 4 && (keyState == 256 || keyState == 257))
        return false;
    else if (softKey == 115 && hardKey == 12 && (keyState == 256 || keyState == 257))
        return false;
    else
        return true;
}
catch (Exception ex)
{
    string dump = ex.Message;
    return true;
}
}

#region Nested type: HookProc

internal delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

#endregion

#region Nested type: KBDLLHOOKSTRUCT

private struct KBDLLHOOKSTRUCT
{
public IntPtr dwExtraInfo;
public int flags;
public int scanCode;
public int time;
public int vkCode;
}

#endregion

这是一个快速而肮脏的解决方案,您可能希望在使用前清理:) 只需调用HookKeyboardEvent(true)以启用钩子并使HookKeyboardEvent(false)取消挂钩。

我希望它能解决你的问题。