locked
Control Panel, Accessibility options, Filterkeys - SystemParametersInfo RRS feed

  • Question

  • Hello everyone.

    I'm busy with a project that deals with some Accessibility ( Control Panel ) settings. What I'm trying now, is to get the Repeat Delay for the FilterKeys on my system. I did this :

    Code Snippet
    Imports System.Runtime.InteropServices
    Public Class Form1
        Private Const SPI_GETFILTERKEYS = 50
        Private SPI_SETFILTERKEYS As Integer = 51
        Private FKF_FILTERKEYSON As Integer = 1

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure FILTERKEYS
            Public cbSize As UInt16
            Public dwFlags As Integer
            Public iWaitMSec As Integer
            Public iDelayMSec As Integer
            Public iRepeatMSec As Integer
            Public iBounceMSec As Integer

        End Structure

        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Public Shared Function SystemParametersInfo(ByVal uiAction As Integer, ByVal uiParam As Integer, ByVal pvParam As IntPtr, ByVal fuWinIni As Integer) As Boolean
        End Function
        Private oldfilterkeys As New FILTERKEYS()

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim size As Integer = oldfilterkeys.cbSize = Marshal.SizeOf(GetType(FILTERKEYS))
            ' Allocate A Temporary Memory Block And Get The Pointer (Unsafe)

            Dim metrics As IntPtr = Marshal.AllocHGlobal(size)

            ' Copy The Initial Structure.

            Marshal.StructureToPtr(oldfilterkeys, metrics, True)
            Dim result As Boolean = SystemParametersInfo(SPI_GETFILTERKEYS, Marshal.SizeOf(GetType(FILTERKEYS)), metrics, 0)

            ' Copy The Return Structure.

            Dim FFK As FILTERKEYS = DirectCast(Marshal.PtrToStructure(metrics, GetType(FILTERKEYS)), FILTERKEYS)


            ' Show The  Value.

            MessageBox.Show(FFK.iDelayMSec.ToString)

            ' Clean Up Allocated Memory.

            Marshal.FreeHGlobal(metrics)
        End Sub
    End Class

     

     

    But it keeps returning 0, instead of the real amount.
    Can anyone help me ¿

    Hannes
    Wednesday, February 27, 2008 1:18 PM

Answers

  • Hi Hannes,

    I modified your code to the following code snippet. In this scenario the improper parameter marshaling doesn't call this function successfully. I read the code exmaple from here to modify your code. Now it works properly. Hope this helps.

    Imports System.Runtime.InteropServices
    Public Class Form1
        Private Const SPI_GETFILTERKEYS = 50
        Private SPI_SETFILTERKEYS As Integer = 51
        Private FKF_FILTERKEYSON As Integer = 1
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure FILTERKEYS
            Public cbSize As UInt16
            Public dwFlags As Integer
            Public iWaitMSec As Integer
            Public iDelayMSec As Integer
            Public iRepeatMSec As Integer
            Public iBounceMSec As Integer
        End Structure
        <DllImport("user32", CharSet:=CharSet.Auto)> _
        Private Shared Function SystemParametersInfo(ByVal uAction As Integer, ByVal uParam As Integer, ByRef lpvParam As FILTERKEYS, ByVal fuWinIni As Integer) As Integer
        End Function
        Private oldfilterkeys As New FILTERKEYS()
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            oldfilterkeys.cbSize = Marshal.SizeOf(GetType(FILTERKEYS))
            Dim result As Boolean = SystemParametersInfo(SPI_GETFILTERKEYS, 0, oldfilterkeys, 0)
            MessageBox.Show(result.ToString)
            MessageBox.Show(oldfilterkeys.iDelayMSec.ToString)
        End Sub
    End Class

    Best regards,
    Riquel
    Monday, March 3, 2008 2:53 PM
    Moderator