Hi All,
i have been teaching myself for a few days how to code in visual basic and i am writing a small program that will help me at work. i have got the program to turn on the caps with the program i need and off when i am not in it but the problem i have now is that when i am out of the program and want to use caps i cant because my program keeps turning them back off. i just cant think of another way to do what i have done. any feedback would be appreciated :)
p.s. i realise some of this may be sloppy but i am a newbie. also i DO NOT take credit for everything in this (such as finding current active window). not sure if i am permitted to link where i got it from so have left it out.
i have been teaching myself for a few days how to code in visual basic and i am writing a small program that will help me at work. i have got the program to turn on the caps with the program i need and off when i am not in it but the problem i have now is that when i am out of the program and want to use caps i cant because my program keeps turning them back off. i just cant think of another way to do what i have done. any feedback would be appreciated :)
p.s. i realise some of this may be sloppy but i am a newbie. also i DO NOT take credit for everything in this (such as finding current active window). not sure if i am permitted to link where i got it from so have left it out.
Code:
'######## Autocaps v1.0 By Paul O'Connor ###########
Option Explicit Off
Option Strict Off
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Public Class Form1
Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Integer
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
'#### Welcome Box Popup - Loads at Start - not yet active ####
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
'#### Timer to keep reading which window is active ####
Dim vrbWindow As String
Public Sub timerActiveWindowCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerActiveWindowCheck.Tick
' Get the Handle to the Current Forground Window
Dim hWnd As IntPtr = GetForegroundWindow()
If hWnd = IntPtr.Zero Then Exit Sub
' Find the Length of the Windows Title
Dim TitleLength As Integer
TitleLength = GetWindowTextLength(hWnd)
' Find the Windows Title
Dim WindowTitle As String = StrDup(TitleLength + 1, "*")
GetWindowText(hWnd, WindowTitle, TitleLength + 1)
' Find the PID of the Application that Owns the Window
Dim pid As Integer = 0
GetWindowThreadProcessId(hWnd, pid)
If pid = 0 Then Exit Sub
' Get the actual PROCESS from the process ID
Dim proc As Process = Process.GetProcessById(pid)
If proc Is Nothing Then Exit Sub
Label6.Text = pid.ToString ' ##### might use this later #####
Label3.Text = proc.ProcessName ' ##### might use this later #####
Label4.Text = proc.MainWindowTitle '##### might use this later #####
lblCWindow.Text = WindowTitle
Label7.Text = TitleLength.ToString ' ##### might use this later #####
End Sub
'#### checkbox to start/stop Autocaps - output to labels for status####
Public Sub chbAutocaps_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbAutocaps.CheckedChanged
If chbAutocaps.Checked = True Then
lblACapStatus.Text = "On"
Else
lblACapStatus.Text = "Off"
End If
End Sub
'#### checks caps lock status and output to label ####
Private Sub timerCapsCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerCapsCheck.Tick
If My.Computer.Keyboard.CapsLock Then
lblCapsStatus.Text = "On"
Else
lblCapsStatus.Text = "Off"
End If
End Sub
'#### timer to check label with current window process name and execute caps on if it is SA ####
Private Sub timerIdea_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerIdea.Tick
If Label3.Text = "seal5" And My.Computer.Keyboard.CapsLock = False Then
Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 1, 0)
Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 3, 0)
End If
End Sub
'#### timer to check label with current window process name and execute caps off if not SA - bugged, forces caps off continuously ####
Public Sub tmrCapsOff_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCapsOff.Tick
If Label3.Text <> "seal5" And My.Computer.Keyboard.CapsLock = True Then
Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 1, 0)
Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 3, 0)
End If
End Sub
End Class