Whats wrong with this code?

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
I am making an app that hides and shows the highest window via hotkeys. I am now trying to make a user selected list of hotkeys, so the user can change the hotkeys.
I am using this code for the declarations:
VB.NET:
Imports Microsoft.Win32
Imports System.Diagnostics
Imports System.Reflection.Assembly

Public Class Main

    Private Declare Function GetForegroundWindow Lib "user32" () As Integer
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Int32, ByVal nCmdShow As Int32) As Boolean
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Public Declare Function GetDesktopWindow Lib "user32" () As Integer
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
    Private Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Int32
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long) As Int32
    Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Int32
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Int32) As Int32

    Public Function GetTaskbarWindow() As Long
        GetTaskbarWindow = FindWindow("shell_traywnd", "")
    End Function
I am using this code for the selection:
VB.NET:
   If RadioButton1.Checked = True Then
            My.Settings.Style = "SCAK"
            My.Settings.Save()
        ElseIf RadioButton2.Checked = True Then
            My.Settings.Style = "CAK"
            My.Settings.Save()
        ElseIf RadioButton3.Checked = True Then
            My.Settings.Style = "SCUD"
            My.Settings.Save()
        ElseIf RadioButton4.Checked = True Then
            My.Settings.Style = "Function"
            My.Settings.Save()
        ElseIf RadioButton5.Checked = True Then
            My.Settings.Style = "CTRLALTZ"
            My.Settings.Save()
        ElseIf RadioButton6.Checked = True Then
            My.Settings.Style = "CTRLSHIFT-+"
            My.Settings.Save()
        ElseIf RadioButton7.Checked = True Then
            My.Settings.Style = "CTRLALTUPDOWN"
            My.Settings.Save()
        End If
        My.Settings.Save()
        My.Settings.Reload()
and this code for reading what the selection is:
VB.NET:
    Dim HandleToHide(200) As IntPtr
    Dim i As Integer

    Dim Key1 As Boolean
    Dim Key2 As Boolean
    Dim Key3 As Boolean

    Dim ShowKey As Boolean
    Dim HideKey As Boolean

    Private Sub HotKeyPress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetHotKeyPress.Tick

 Call CheckKey()

  If Key1 And Key2 And Key3 And HideKey = True Then
                If i = 200 Then
                    Exit Sub
                ElseIf i <= 199 Then
                    If GetForegroundWindow = GetTaskbarWindow() Then
                        Exit Sub
                    End If
                    If GetForegroundWindow = CheckForDesktopWindow() Then
                        Exit Sub
                    End If
                    i = i + 1
                    HandleToHide(i) = GetForegroundWindow()
                    ShowWindow(HandleToHide(i), SW_Hide)
                End If
            ElseIf Key1 And Key2 And Key3 And ShowKey = True Then
                If i = 0 Then
                    Exit Sub
                ElseIf i >= 1 Then
                    BringWindowToTop(HandleToHide(i))
                    ShowWindow(HandleToHide(i), SW_ShowNormal)
                    BringWindowToTop(HandleToHide(i))
                    SetForegroundWindow(HandleToHide(i))
                    i = i - 1
                End If
            End If
           

Public Sub CheckKey()
        My.Settings.Reload()
        If My.Settings.Style = "CAK" Then
            Key1 = GetAsyncKeyState(Keys.ControlKey)
            Key2 = GetAsyncKeyState(Keys.Menu)
            Key3 = True
            ShowKey = GetAsyncKeyState(Keys.S)
            HideKey = GetAsyncKeyState(Keys.H)
        End If
        If My.Settings.Style = "SCAK" Then
            Key1 = GetAsyncKeyState(Keys.ControlKey)
            Key2 = GetAsyncKeyState(Keys.Menu)
            Key3 = GetAsyncKeyState(Keys.ShiftKey)
            ShowKey = GetAsyncKeyState(Keys.S)
            HideKey = GetAsyncKeyState(Keys.H)
        End If
        If My.Settings.Style = "SCUD" Then
            Key1 = GetAsyncKeyState(Keys.ControlKey)
            Key2 = GetAsyncKeyState(Keys.ShiftKey)
            Key3 = True
            ShowKey = GetAsyncKeyState(Keys.Up)
            HideKey = GetAsyncKeyState(Keys.Down)
        End If
        If My.Settings.Style = "Function" Then
            Key1 = GetAsyncKeyState(Keys.ControlKey)
            Key2 = True
            Key3 = True
            HideKey = GetAsyncKeyState(Keys.F9)
            ShowKey = GetAsyncKeyState(Keys.F10)
        End If
        If My.Settings.Style = "CTRLALTZ" Then
            Key1 = GetAsyncKeyState(Keys.ControlKey)
            Key1 = GetAsyncKeyState(Keys.Menu)
            Key3 = True
            HideKey = GetAsyncKeyState(Keys.Z)
            ShowKey = GetAsyncKeyState(Keys.Oemplus)
        End If
        If My.Settings.Style = "CTRLSHIFT-+" Then
            Key1 = GetAsyncKeyState(Keys.ControlKey)
            Key1 = GetAsyncKeyState(Keys.ShiftKey)
            Key3 = True
            HideKey = GetAsyncKeyState(Keys.OemMinus)
            ShowKey = GetAsyncKeyState(Keys.Oemplus)
        End If
        If My.Settings.Style = "CTRLALTUPDOWN" Then
            Key1 = GetAsyncKeyState(Keys.ControlKey)
            Key1 = GetAsyncKeyState(Keys.Menu)
            Key3 = True
            HideKey = GetAsyncKeyState(Keys.Down)
            ShowKey = GetAsyncKeyState(Keys.Up)
        End If
    End Sub

I know that that code is probably quite disjointed, i just copied and pasted in the important sections, but the problem is, if i select radio button 5, 6 or 7, it doesnt work unless i have previously selected radio button 4, i find.

I am very confused about this problem, and looking over my code, i dont think there is a problem, but there must be.

thanks :)
 
Back
Top