这篇msdn文章 - http://msdn.microsoft.com/en-us/library/bb219746(VS.85).aspx#Disabling_Accessibility_Shortcut_Keys - 为C ++程序员提供了有关如何暂时禁用可访问性的Windows快捷方式的信息(例如按住Shift 8秒,或快速连续按下Shift超过5次)
当然在C#中有一些简单的方法可以做到这一点,但我找不到任何资源。我在非全屏应用程序中使用DirectInput。
我想做的就是没有恼人的弹出窗口出现;我更喜欢不需要使用Windows设置的东西,以防万一应用程序以非优雅的方式关闭(我不希望在这些情况下永久更改用户的设置)
有什么想法吗?
答案 0 :(得分:6)
以防万一其他人需要这个,这里是转换后的C#代码,最后确实有效:
[DllImport( "user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = false )]
private static extern bool SystemParametersInfo( uint action, uint param,
ref SKEY vparam, uint init );
[DllImport( "user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = false )]
private static extern bool SystemParametersInfo( uint action, uint param,
ref FILTERKEY vparam, uint init );
private const uint SPI_GETFILTERKEYS = 0x0032;
private const uint SPI_SETFILTERKEYS = 0x0033;
private const uint SPI_GETTOGGLEKEYS = 0x0034;
private const uint SPI_SETTOGGLEKEYS = 0x0035;
private const uint SPI_GETSTICKYKEYS = 0x003A;
private const uint SPI_SETSTICKYKEYS = 0x003B;
private static bool StartupAccessibilitySet = false;
private static SKEY StartupStickyKeys;
private static SKEY StartupToggleKeys;
private static FILTERKEY StartupFilterKeys;
private const uint SKF_STICKYKEYSON = 0x00000001;
private const uint TKF_TOGGLEKEYSON = 0x00000001;
private const uint SKF_CONFIRMHOTKEY = 0x00000008;
private const uint SKF_HOTKEYACTIVE = 0x00000004;
private const uint TKF_CONFIRMHOTKEY = 0x00000008;
private const uint TKF_HOTKEYACTIVE = 0x00000004;
private const uint FKF_CONFIRMHOTKEY = 0x00000008;
private const uint FKF_HOTKEYACTIVE = 0x00000004;
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Auto )]
public struct SKEY
{
public uint cbSize;
public uint dwFlags;
}
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Auto )]
public struct FILTERKEY
{
public uint cbSize;
public uint dwFlags;
public uint iWaitMSec;
public uint iDelayMSec;
public uint iRepeatMSec;
public uint iBounceMSec;
}
private static uint SKEYSize = sizeof( uint ) * 2;
private static uint FKEYSize = sizeof( uint ) * 6;
public static void ToggleAccessibilityShortcutKeys( bool ReturnToStarting )
{
if ( !StartupAccessibilitySet )
{
StartupStickyKeys.cbSize = Configuration.SKEYSize;
StartupToggleKeys.cbSize = Configuration.SKEYSize;
StartupFilterKeys.cbSize = Configuration.FKEYSize;
SystemParametersInfo( SPI_GETSTICKYKEYS, SKEYSize, ref StartupStickyKeys, 0 );
SystemParametersInfo( SPI_GETTOGGLEKEYS, SKEYSize, ref StartupToggleKeys, 0 );
SystemParametersInfo( SPI_GETFILTERKEYS, FKEYSize, ref StartupFilterKeys, 0 );
StartupAccessibilitySet = true;
}
if ( ReturnToStarting )
{
// Restore StickyKeys/etc to original state and enable Windows key
SystemParametersInfo( SPI_SETSTICKYKEYS, SKEYSize, ref StartupStickyKeys, 0 );
SystemParametersInfo( SPI_SETTOGGLEKEYS, SKEYSize, ref StartupToggleKeys, 0 );
SystemParametersInfo( SPI_SETFILTERKEYS, FKEYSize, ref StartupFilterKeys, 0 );
}
else
{
// Disable StickyKeys/etc shortcuts but if the accessibility feature is on,
// then leave the settings alone as its probably being usefully used
SKEY skOff = StartupStickyKeys;
//if ( ( skOff & SKF_STICKYKEYSON ) == 0 )
{
// Disable the hotkey and the confirmation
skOff.dwFlags &= ~SKF_HOTKEYACTIVE;
skOff.dwFlags &= ~SKF_CONFIRMHOTKEY;
SystemParametersInfo( SPI_SETSTICKYKEYS, SKEYSize, ref skOff, 0 );
}
SKEY tkOff = StartupToggleKeys;
//if ( ( tkOff & TKF_TOGGLEKEYSON ) == 0 )
{
// Disable the hotkey and the confirmation
tkOff.dwFlags &= ~TKF_HOTKEYACTIVE;
tkOff.dwFlags &= ~TKF_CONFIRMHOTKEY;
rs = SystemParametersInfo( SPI_SETTOGGLEKEYS, SKEYSize, ref tkOff, 0 );
}
FILTERKEY fkOff = StartupFilterKeys;
//if ( ( fkOff & FKF_FILTERKEYSON ) == 0 )
{
// Disable the hotkey and the confirmation
fkOff.dwFlags &= ~FKF_HOTKEYACTIVE;
fkOff.dwFlags &= ~FKF_CONFIRMHOTKEY;
SystemParametersInfo( SPI_SETFILTERKEYS, FKEYSize, ref fkOff, 0 );
}
}
}
请注意,我无法从C ++中转换出三个IF语句(这些语句已被注释掉)。微软推荐这些,但我不知道如何使它们在C#中工作。另外,我没有在结构上使用sizeof()(而是为它们的大小手动创建变量),因为这样做需要不安全的代码,我不希望这是我的特定程序的要求。
答案 1 :(得分:5)
感谢那些在我的XNA游戏中进行了一些微小的整理,以防止粘键弹出。
以下是完成的代码:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using System.Runtime.InteropServices;
namespace Engine
{
#if WINDOWS
/// <summary>
/// Helper for Windows to temporarily disable the popup caused by the
/// Accessibility features.
/// See: http://stackoverflow.com/questions/734618/disabling-accessibility-shortcuts-in-net-application
/// and: http://msdn.microsoft.com/en-us/library/ee416808(v=vs.85).aspx
/// </summary>
public class WindowsHelperAccessibilityKeys
{
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = false)]
private static extern bool SystemParametersInfo(uint action, uint param,
ref SKEY vparam, uint init);
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = false)]
private static extern bool SystemParametersInfo(uint action, uint param,
ref FILTERKEY vparam, uint init);
private const uint SPI_GETFILTERKEYS = 0x0032;
private const uint SPI_SETFILTERKEYS = 0x0033;
private const uint SPI_GETTOGGLEKEYS = 0x0034;
private const uint SPI_SETTOGGLEKEYS = 0x0035;
private const uint SPI_GETSTICKYKEYS = 0x003A;
private const uint SPI_SETSTICKYKEYS = 0x003B;
private static bool StartupAccessibilitySet = false;
private static SKEY StartupStickyKeys;
private static SKEY StartupToggleKeys;
private static FILTERKEY StartupFilterKeys;
private const uint SKF_STICKYKEYSON = 0x00000001;
private const uint TKF_TOGGLEKEYSON = 0x00000001;
private const uint SKF_CONFIRMHOTKEY = 0x00000008;
private const uint SKF_HOTKEYACTIVE = 0x00000004;
private const uint TKF_CONFIRMHOTKEY = 0x00000008;
private const uint TKF_HOTKEYACTIVE = 0x00000004;
private const uint FKF_CONFIRMHOTKEY = 0x00000008;
private const uint FKF_HOTKEYACTIVE = 0x00000004;
private const uint FKF_FILTERKEYSON = 0x00000001;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SKEY
{
public uint cbSize;
public uint dwFlags;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct FILTERKEY
{
public uint cbSize;
public uint dwFlags;
public uint iWaitMSec;
public uint iDelayMSec;
public uint iRepeatMSec;
public uint iBounceMSec;
}
private static uint SKEYSize = sizeof(uint) * 2;
private static uint FKEYSize = sizeof(uint) * 6;
/// <summary>
/// False to stop the sticky keys popup.
/// True to return to whatever the system has been set to.
/// </summary>
public static void AllowAccessibilityShortcutKeys(bool bAllowKeys)
{
if (!StartupAccessibilitySet)
{
StartupStickyKeys.cbSize = SKEYSize;
StartupToggleKeys.cbSize = SKEYSize;
StartupFilterKeys.cbSize = FKEYSize;
SystemParametersInfo(SPI_GETSTICKYKEYS, SKEYSize, ref StartupStickyKeys, 0);
SystemParametersInfo(SPI_GETTOGGLEKEYS, SKEYSize, ref StartupToggleKeys, 0);
SystemParametersInfo(SPI_GETFILTERKEYS, FKEYSize, ref StartupFilterKeys, 0);
StartupAccessibilitySet = true;
}
if (bAllowKeys)
{
// Restore StickyKeys/etc to original state and enable Windows key
SystemParametersInfo(SPI_SETSTICKYKEYS, SKEYSize, ref StartupStickyKeys, 0);
SystemParametersInfo(SPI_SETTOGGLEKEYS, SKEYSize, ref StartupToggleKeys, 0);
SystemParametersInfo(SPI_SETFILTERKEYS, FKEYSize, ref StartupFilterKeys, 0);
}
else
{
// Disable StickyKeys/etc shortcuts but if the accessibility feature is on,
// then leave the settings alone as its probably being usefully used
SKEY skOff = StartupStickyKeys;
if ( ( skOff.dwFlags & SKF_STICKYKEYSON ) == 0 )
{
// Disable the hotkey and the confirmation
skOff.dwFlags &= ~SKF_HOTKEYACTIVE;
skOff.dwFlags &= ~SKF_CONFIRMHOTKEY;
SystemParametersInfo(SPI_SETSTICKYKEYS, SKEYSize, ref skOff, 0);
}
SKEY tkOff = StartupToggleKeys;
if ( ( tkOff.dwFlags & TKF_TOGGLEKEYSON ) == 0 )
{
// Disable the hotkey and the confirmation
tkOff.dwFlags &= ~TKF_HOTKEYACTIVE;
tkOff.dwFlags &= ~TKF_CONFIRMHOTKEY;
SystemParametersInfo(SPI_SETTOGGLEKEYS, SKEYSize, ref tkOff, 0);
}
FILTERKEY fkOff = StartupFilterKeys;
if ( ( fkOff.dwFlags & FKF_FILTERKEYSON ) == 0 )
{
// Disable the hotkey and the confirmation
fkOff.dwFlags &= ~FKF_HOTKEYACTIVE;
fkOff.dwFlags &= ~FKF_CONFIRMHOTKEY;
SystemParametersInfo(SPI_SETFILTERKEYS, FKEYSize, ref fkOff, 0);
}
}
}
}
#endif
}
我在游戏开始时使用参数设置为false,并在游戏退出之前将参数设置为true:
/// <summary>
/// This is the preferred way to return to the operating system.
/// </summary>
public void ExitAndTidyUP()
{
#if WINDOWS
WindowsHelperAccessibilityKeys.AllowAccessibilityShortcutKeys(true);
#endif
Exit();
}
据我所知,它完美无缺。
此致
答案 2 :(得分:2)
您必须执行与您引用的链接相同的操作。可以通过P / Invoke层调用SystemParametersInfo API函数,您可以在此处找到定义:
http://www.pinvoke.net/default.aspx/user32/SystemParametersInfo.html
答案 3 :(得分:2)
请注意上面发布的C#代码: 您可以通过AND与结构的flags字段转换这些问题行,如下所示: if((skOff.dwFlags&amp; SKF_STICKYKEYSON)== 0) 您需要添加以下行: private const uint FKF_FILTERKEYSON = 0x00000001; 在const定义下也是如此。
答案 4 :(得分:0)
你也可以看一下C#
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
private const int MYKEYID = 0; // In case you want to register more than one...
public Form1() {
InitializeComponent();
RegisterHotKey(this.Handle, MYKEYID, MOD_ALT, Keys.Tab);
this.FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
UnregisterHotKey(this.Handle, MYKEYID);
}
protected override void WndProc(ref Message m) {
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == MYKEYID) {
Console.Beep();
}
base.WndProc(ref m);
}
// P/Invoke declarations
private const int WM_HOTKEY = 0x312;
private const int MOD_ALT = 1;
private const int MOD_CONTROL = 2;
private const int MOD_SHIFT = 4;
[DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hWnd, int id, int modifier, Keys vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
}
欢呼声