hook windows key.

hi,

could anybody write me a little code that hooks windows keys and add an event when the windows keys are pressed to be able to run my code...

it'd be a great help, so plz take time to help me out

Thank you;




Answer this question

hook windows key.

  • kuntushi

    thank you, Bruno... where should i paste this code sample

  • chandu_sanka

    Hello Richard,

    could you write me a code-sample anyways, thnx for your help.



  • Tadwick

    ok, that's all rite, but this doesn't hook the windows key, it just tells us if it was pressed... i need windows not to react on pressing the win keys, and instead of windows, i want my app to act.

  • Prak

    Attila Fogel,

    This code snippet is just a code-sample on the WndProc method to help you understand this kind of method. Please use it ptoperly in your own application while you are using the WndProc method to accomplish your project.



  • AlucardHellSing

     

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    Select Case e.KeyCode

    'Left windows key was pressed!!

    Case Keys.LWin

    TextBox1.Text = "Left windows key was pressed!!"

    '____________________________________________

    'Right windows key was pressed!!

    Case Keys.RWin

    TextBox1.Text = "Right windows key was pressed!!"

    End Select

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.KeyPreview = True

    Me.StartPosition = FormStartPosition.CenterScreen

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    MsgBox("The event fired was textbox1.text was changed!!")

    End Sub

     

    Hi,

    The above will check for either windows key being pressed and shows a messageBox on the event of the textbox text being changed.

    Note: In the FORM load event i have set the keyPreview property to TRUE, this is needed but you could set that property in the properties window of the FORM.

     I have also set the start position to CenterScreen

     

    Regards,

    S_DS

     



  • Nick Gravelyn

    Hi

    The control class (from which form inherits) exposes a protected WndProc method that you can use to preprocess windows messages ....

    If you are not in a position to override this method ... you can still interop and hook the old fashioned way.

    Richard


  • LKeene

    Attila Fogel,

    The program overrides the form's WndProc subroutine to look for Windows messages. When it sees a WM_SIZING message, it uses Marshal.PtrToStructure to convert the LParam parameter into a Rect structure containing information about the resizing.

    Imports System.Runtime.InteropServices

    ...

    Public Structure Rect

    Public left As Integer

    Public top As Integer

    Public right As Integer

    Public bottom As Integer

    End Structure

    Protected Overrides Sub WndProc(ByRef m As _

    System.Windows.Forms.Message)

    Const WM_SIZING As Long = &H214

    Const WMSZ_LEFT As Integer = 1

    Const WMSZ_RIGHT As Integer = 2

    Const WMSZ_TOP As Integer = 3

    Const WMSZ_TOPLEFT As Integer = 4

    Const WMSZ_TOPRIGHT As Integer = 5

    Const WMSZ_BOTTOM As Integer = 6

    Const WMSZ_BOTTOMLEFT As Integer = 7

    Const WMSZ_BOTTOMRIGHT As Integer = 8

    If m.Msg = WM_SIZING And m.HWnd.Equals(Me.Handle) Then

    ' Turn the message's lParam into a Rect.

    Dim r As Rect

    r = DirectCast( _

    Marshal.PtrToStructure(m.LParam, _

    GetType(Rect)), _

    Rect)

    ' Get the desired height and width.

    Dim hgt As Integer = r.bottom - r.top

    Dim wid As Integer = r.right - r.left

    ' Make height and width multiples of 50 pixels.

    hgt = 50 * (CInt(hgt / 50))

    wid = 50 * (CInt(wid / 50))

    ' See if the user is dragging the top edge.

    If m.WParam.ToInt32 = WMSZ_TOP Or _

    m.WParam.ToInt32 = WMSZ_TOPLEFT Or _

    m.WParam.ToInt32 = WMSZ_TOPRIGHT _

    Then

    ' Reset the top.

    r.top = r.bottom - hgt

    Else

    ' Reset the height to the saved value.

    r.bottom = r.top + hgt

    End If

    ' See if the user is dragging the left edge.

    If m.WParam.ToInt32 = WMSZ_LEFT Or _

    m.WParam.ToInt32 = WMSZ_TOPLEFT Or _

    m.WParam.ToInt32 = WMSZ_BOTTOMLEFT _

    Then

    ' Reset the left.

    r.left = r.right - wid

    Else

    ' Reset the width to the saved value.

    r.right = r.left + wid

    End If

    ' Update the Message object's LParam field.

    Marshal.StructureToPtr(r, m.LParam, True)

    End If

    MyBase.WndProc(m)

    End Sub



  • hook windows key.