C# 如何在Motorola MC75上禁用发送键和结束键

C# 如何在Motorola MC75上禁用发送键和结束键,c#,windows-mobile,motorola,C#,Windows Mobile,Motorola,如何在摩托罗拉MC75上禁用发送键和结束键 我需要任何C#示例代码 提前感谢编辑:我以前不知道PaulH发布的“AllKeys”解决方案,这应该是比我发布的更好的解决方案。 我假设你想处理绿色和红色的硬件键?通话和挂断键 如果是这种情况,您可以监视keyevents,如果它们符合您的条件,则选择不将它们传递到windows private const int WH_KEYBOARD_LL = 20; private static int _hookHandle; private HookProc

如何在摩托罗拉MC75上禁用发送键和结束键

我需要任何C#示例代码


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

我假设你想处理绿色和红色的硬件键?通话和挂断键

如果是这种情况,您可以监视keyevents,如果它们符合您的条件,则选择不将它们传递到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)解除钩子


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

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

我假设你想处理绿色和红色的硬件键?通话和挂断键

如果是这种情况,您可以监视keyevents,如果它们符合您的条件,则选择不将它们传递到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)解除钩子

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

我在网上回答了这个问题

您可以使用AllKeys API来实现这一点

在C#中使用它的p/Invoke签名如下:

关于它的用法,这里有一个很好的一般性解释:

我在网上回答了这个问题

您可以使用AllKeys API来实现这一点

在C#中使用它的p/Invoke签名如下:


关于它的用法,这里有一个很好的一般性解释:

使用Motorola AppCenter来限制正在运行的应用程序。它允许您阻止键、程序等。

使用摩托罗拉AppCenter限制正在运行的应用程序。它允许您阻止键、程序等。

您可以指定为什么需要它吗?这将有助于引导答案。我需要禁用摩托罗拉MC75中的[结束]电话呼叫按钮。嗯,似乎6.5中的软键布局已经发生了很大变化。我所能做的就是为您指出注册表项的方向:
[HKCU\Software\Microsoft\Shell]
[HKLM\Software\Microsoft\CHome\]
,并使用试错法使其正常工作。也许有更好的解决方案。你能具体说明为什么需要它吗?这将有助于引导答案。我需要禁用摩托罗拉MC75中的[结束]电话呼叫按钮。嗯,似乎6.5中的软键布局已经发生了很大变化。我所能做的就是为您指出注册表项的方向:
[HKCU\Software\Microsoft\Shell]
[HKLM\Software\Microsoft\CHome\]
,并使用试错法使其正常工作。也许有更好的解决方案。键盘挂钩不允许阻止键盘消息的传递或修改键盘消息。它们只能用于在按键时提醒您。不传递它们只会阻止系统中的其他键盘挂钩接收消息。这是一个坏主意,可能会导致其他应用程序崩溃。我使用键盘挂钩处理很多不同的东西:将它们设为NULL、替换为其他等等(请参阅我的网站)。你从哪里得到这些信息?顺便说一句:在WM上只允许一个钩子。键盘钩子不允许阻止键盘消息的传递或修改键盘消息。它们只能用于在按键时提醒您。不传递它们只会阻止系统中的其他键盘挂钩接收消息。这是一个坏主意,可能会导致其他应用程序崩溃。我使用键盘挂钩处理很多不同的东西:将它们设为NULL、替换为其他等等(请参阅我的网站)。你从哪里得到这些信息?顺便说一句:在WM上只允许一个钩子。dale b.的评论:“不幸的是,AllKeys解决方案也不能给予应用程序完全控制。应用程序不能使用AllKeys接收功能键F1和F2。”dale b.的评论:“不幸的是,AllKeys解决方案也没有给予应用程序完全控制权。应用程序无法使用所有键接收功能键F1和F2。“