Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Capturing F keystroke inside a form in VB.NET

Posted on 2011-09-13
4
Medium Priority
?
446 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 1600 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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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