Solved

Capturing F keystroke inside a form in VB.NET

Posted on 2011-09-13
4
442 Views
Last Modified: 2012-05-12
Hi,

Is it possible to capture, for example, F5 keystroke on a form even if the focus is on a textbox?

I need to capture F5 on a form independently of the control that has the focus.

Thanks!
0
Comment
Question by:pvg1975
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 400 total points
ID: 36530936
The simple way to do this is using form.keypreview.  Setting this property true sends all key presses to the form level  handler:
Form.KeyPreview Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.F5 Then
            MsgBox("F5")
            'If this is false, then keypress is sent on to focus control
            e.Handled = True
        End If


    End Sub


End Class

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 100 total points
ID: 36530949
Enable the KeyPreview property of the form and capture the keypress in the KeyDown event of the form
0
 

Author Closing Comment

by:pvg1975
ID: 36530971
I was missing keypreview.

Thanks guys!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36531014
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question