Solo nel caso qualcuno altro ha bisogno di questo, ecco il codice C# convertito, che finalmente funziona:
[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);
}
}
}
Si noti che non sono riuscito a convertire tre delle istruzioni IF da C++ (quelle sono commentate). Microsoft consiglia quelli, ma non conosco un modo per farli funzionare in C#. Inoltre, non sto usando sizeof() sulle structs (invece di creare manualmente variabili per le loro dimensioni), perché farlo richiede un codice non sicuro, e non voglio che sia un requisito per il mio particolare programma.
Okay ... speravo in qualcosa di non permanente, ma anche questo è buono. Grazie! – x4000
@ x4000: la tua domanda dice che mostra come disabilitare temporaneamente i tasti di scelta rapida. Viene chiamata la stessa API, quindi devi solo strutturare il codice allo stesso modo e dovrebbe anche essere non permanente. – casperOne
Beh, immagino di aver misspoke - ti mostrano come spegnerlo e riaccenderlo, aspettandoti di riaccenderlo quando hai finito. Va tutto bene finché l'applicazione non si blocca. – x4000