Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I'm trying to get to process the keydown message while starting my application

I already had some code that I used to prevent the window from being moved or resized ..

Looking at the Microsoft documentation it seemed to me I could easily expand my existing code
by testing for the correct message sent
below is my code:
The [case WM_NCLBUTTONDOWN ] works perfect - but my [Case WM_KEYDOWN] never enters ???

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const WM_KEYDOWN As Integer = &H100
        Const WM_NCLBUTTONDOWN As Integer = &HA1
        Const HT_CAPTION As Integer = &H2
        Select Case m.Msg

            Case WM_KEYDOWN
                MsgBox("key Pressed")

            Case WM_NCLBUTTONDOWN  'Locking window from resizing & moving
                If KDMStatus.LockWindow_Positions = True Then
                    If m.WParam = New IntPtr(HT_CAPTION) Then
                    End If
                End If
        End Select

    End Sub

What am I missing ? Can I actually trapping the keydown events by using the Message Structure

Updated 7-Jun-13 9:41am
Sergey Alexandrovich Kryukov 7-Jun-13 15:33pm    
Tag it: "WinForms".

If you're talking about having a key held down when your app starts so you can do something special, the WM_KEYDOWN message won't help you. As a matter of fact, no messages wil help you.

The WM_KEYDOWN message is sent only when the key is "made", or goes down. It does not tell you that a key is being held down.

To check keystate you have to PInvoke GetKeyState[^] when your app starts. You can get the state of a few keys if you want. Each key will take a seperate call to GetKeyState.
Share this answer
Preventing a form from resize is its standard option, please see:[^],[^].

Four System.Windows.Forms.FormBorderStyle.Fixed* members of this enumeration provide options with fixed size.

As to preventing a form to move, it would be a huge abuse and a crime against the freedom of your users! Just don't do it, ever.

Therefore, your problem is solved without directly messing with message handling.

[EDIT #1]

As to your question, I can explain what's the reason for not getting to the case WM_KEYDOWN, but you need to check it up with your code which I cannot see, otherwise it won't be 100% certain.

The thing is: you handle only the messages on your form, but you might also have some controls on your form. One of those controls can possess the keyboard focus. In this case, all keyboard events are handled in this control, so they don't reach your form.

For a test, try same code on empty form, then you will see.

[EDIT #2]

Now, the question is: what to do in practice? You could handle keyboard events on all of your focusable controls as well, but you don't need this complication.

And you don't really need to use WndProc to handle keyboard messages. All functionality you possibly can have with those messages, you can by handling some or all of the System.Windows.Forms.Control keyboard events: KeyDown, KeyUp, KeyPress and PreviewKeyDown:[^].

You can handle any of these event in the form, without having the problem with a focused child control which can possess the keyboard focus. Now — attention! this is the key point: for this purpose, you should also assign true to the form property KeyPreview:[^].

I think, by now your question is comprehensively answered.

Share this answer
Georg Kohler 7-Jun-13 15:51pm    
Actually your wrong .. working on an industrial solution most companies don't allow there Operators to play around with the forms ..
Also if you read the question, the removing /resizing is not my "problem" that's working perfect
- I'm wondering if I can actually trap a keydown event like this - it sure looks like you could - it's just "not working"
Sergey Alexandrovich Kryukov 7-Jun-13 16:27pm    
In those "most companies" foolish developers and management are everywhere, so what? Why using fools for learning bad things? This is not an argument at all. You are wrong, sorry.
The fixed position really may make sense: this is the maximized window which shows all needed for production at once, no more, no less. This is the different story.

By the way, a standard way to remove motion is to remove the title bar. You won't need any of the controls on it, anyway, so you can simply draw a "title-like" text in the client area.

Georg Kohler 7-Jun-13 16:48pm    
Not sure why we argue about this..
Do you have anything to say about the actual Question I asked ?
Sergey Alexandrovich Kryukov 7-Jun-13 16:52pm    
Who is arguing? I advised you what do to, based on your question, now you decide what you are going to do.
But please understand: I'm not going to help with anything I think is wrong. And I'm not going to try to convince you. You are the one who needs some solution, not me.
Georg Kohler 7-Jun-13 16:56pm    
I'm confused - What advice ?
You told me not to disable moving /resizing - This has absolutely nothing to do with my question!
what Am I missing ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900