caps lock / minimise

  • 19 years ago

    does anyone know how to get and set the state of the caps lock and num lock keys?


    and i would like to know how to create your own minimize button

  • 19 years ago

    Below is some info from the MSDN library to detect CAPS/INS/Scroll lock state, and toggle it:


    Sample Project
    Start a new Standard EXE project in Visual Basic. Form1 is created by default.


    Add a CommandButton to Form1.



    Add the following code to the General Declarations section of Form1:

    Code:


         ' Declare Type for API call:
         Private Type OSVERSIONINFO
           dwOSVersionInfoSize As Long
           dwMajorVersion As Long
           dwMinorVersion As Long
           dwBuildNumber As Long
           dwPlatformId As Long
           szCSDVersion As String * 128   '  Maintenance string for PSS usage
         End Type


         ' API declarations:


         Private Declare Function GetVersionEx Lib "kernel32" _
            Alias "GetVersionExA" _
            (lpVersionInformation As OSVERSIONINFO) As Long


         Private Declare Sub keybd_event Lib "user32" _
            (ByVal bVk As Byte, _
             ByVal bScan As Byte, _
             ByVal dwFlags As Long, ByVal dwExtraInfo As Long)


         Private Declare Function GetKeyboardState Lib "user32" _
            (pbKeyState As Byte) As Long


         Private Declare Function SetKeyboardState Lib "user32" _
            (lppbKeyState As Byte) As Long


         ' Constant declarations:
         Const VKNUMLOCK = &H90
         Const VK
    SCROLL = &H91
         Const VKCAPITAL = &H14
         Const KEYEVENTF
    EXTENDEDKEY = &H1
         Const KEYEVENTFKEYUP = &H2
         Const VER
    PLATFORMWIN32NT = 2
         Const VERPLATFORMWIN32_WINDOWS = 1





    Add the following code to the Click event of the CommandButton:

    Code:


       Private Sub Command1_Click()
         Dim o As OSVERSIONINFO
         Dim NumLockState As Boolean
         Dim ScrollLockState As Boolean
         Dim CapsLockState As Boolean


         o.dwOSVersionInfoSize = Len(o)
         GetVersionEx o
         Dim keys(0 To 255) As Byte
         GetKeyboardState keys(0)


         ' NumLock handling:
         NumLockState = keys(VKNUMLOCK)
         If NumLockState <> True Then    'Turn numlock on
           If o.dwPlatformId = VER
    PLATFORMWIN32WINDOWS Then  '=== Win95/98


             keys(VKNUMLOCK) = 1
             SetKeyboardState keys(0)
           ElseIf o.dwPlatformId = VER
    PLATFORMWIN32NT Then   '=== WinNT
           'Simulate Key Press
             keybdevent VKNUMLOCK, &H45, KEYEVENTFEXTENDEDKEY Or 0, 0
           'Simulate Key Release
             keybd
    event VKNUMLOCK, &H45, KEYEVENTFEXTENDEDKEY _
                Or KEYEVENTF_KEYUP, 0
           End If
         End If


         ' CapsLock handling:
         CapsLockState = keys(VKCAPITAL)
         If CapsLockState <> True Then    'Turn capslock on
           If o.dwPlatformId = VER
    PLATFORMWIN32WINDOWS Then  '=== Win95/98
             keys(VKCAPITAL) = 1
             SetKeyboardState keys(0)
           ElseIf o.dwPlatformId = VER
    PLATFORMWIN32NT Then   '=== WinNT
           'Simulate Key Press
             keybdevent VKCAPITAL, &H45, KEYEVENTFEXTENDEDKEY Or 0, 0
           'Simulate Key Release
             keybd
    event VKCAPITAL, &H45, KEYEVENTFEXTENDEDKEY _
                Or KEYEVENTF_KEYUP, 0
           End If
         End If


         ' ScrollLock handling:
         ScrollLockState = keys(VKSCROLL)
         If ScrollLockState <> True Then    'Turn Scroll lock on
           If o.dwPlatformId = VER
    PLATFORMWIN32WINDOWS Then  '=== Win95/98
             keys(VKSCROLL) = 1
             SetKeyboardState keys(0)
           ElseIf o.dwPlatformId = VER
    PLATFORMWIN32NT Then   '=== WinNT
           'Simulate Key Press
             keybdevent VKSCROLL, &H45, KEYEVENTFEXTENDEDKEY Or 0, 0
           'Simulate Key Release
             keybd
    event VKSCROLL, &H45, KEYEVENTFEXTENDEDKEY _
               Or KEYEVENTF_KEYUP, 0
           End If
         End If
       End Sub





    Press the F5 key to run the program. Click the CommandButton. The state of the CAPS LOCK, the NUM LOCK, and the SCROLL LOCK keys should all be "on."

  • 19 years ago

    To minimize a form, simply set its WindowState to vbMinimized

  • 19 years ago

    To create your own minimize button, you can use an Image control and set the image.Picture property to the picture that you want to display. You can create the picture with Microsoft's Paint, or Adobe's Photoshop, or Jasc's Paint Shop Pro, or some other. Remember that is better to make the picture of the size that the image control will have on the form, because setting the image.Stretch property to True can make the picture look different.
    When you have all this, you can use the Click event of the image control to minimize the form, for example:
    Private Sub Image1_Click()
       Me.WindowState = 1  ' Or vbMinimized
    End Sub
    Also set the image.Tooltip property to the caption that will be displayed when the user puts the mouse over the 'button'.
    You can also use the MouseMove event to change the image.Picture property to add another feature to your app.
    To obtain the same results you can use the Picture control, but I don't recommend it in this case because it is 'heavier' than the Image control.
    In case you need further explanation, please send me an email.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor