Results 1 to 7 of 7

Thread: keypress problems

  1. #1

    Thread Starter
    Hyperactive Member boku's Avatar
    Join Date
    Dec 2004
    Posts
    386

    Red face keypress problems

    I'm trying to build a calculator and im really struggling to get to grips on the vb.net 2008 keypress ... dunno why! anyway

    Code:
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    
    If e.KeyChar = Chr(Keys.NumPad0) Then
                Button0.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad1) Then
                Button1.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad2) Then
                Button2.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad3) Then
                Button3.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad4) Then
                Button4.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad5) Then
                Button5.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad6) Then
                Button6.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad7) Then
                Button7.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad8) Then
                Button8.PerformClick()
            ElseIf e.KeyChar = Chr(Keys.NumPad9) Then
                Button9.PerformClick()
            End If
    
    End Sub
    This doesn't seem to work, i will also need it to capture for the plus, minus, divide and subtract buttons along with the return and enter buttons (for equals)

    Any help please?
    -BoKu-

  2. #2
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: keypress problems

    Well it's quite simple, controls (like the form) only recieve key presses when they have focus, when you have buttons or other input controls on your form, they take focus, any keypress event will goto them and not the form.

    You will need keyboard hooks, or maybe a context menu or other control can do it.

  3. #3
    New Member
    Join Date
    Dec 2007
    Location
    Mumbai,India
    Posts
    15

    Re: keypress problems

    u Can write code in ProcessCmdKey function which is overidable
    write ur all code there and u will get desire output which u want

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: keypress problems

    I don't have a processcmdkey event type (vb05) is this a new feature to .net 2008 ?

  5. #5

    Thread Starter
    Hyperactive Member boku's Avatar
    Join Date
    Dec 2004
    Posts
    386

    Re: keypress problems

    for Phill64

    Visual Basic (Declaration)
    Code:
    <SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.UnmanagedCode)> _
    <SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags := SecurityPermissionFlag.UnmanagedCode)> _
    Protected Overridable Function ProcessCmdKey ( _
    	ByRef msg As Message, _
    	keyData As Keys _
    ) As Boolean
    Visual Basic (Usage)
    Code:
    Dim msg As Message
    Dim keyData As Keys
    Dim returnValue As Boolean
    
    returnValue = Me.ProcessCmdKey(msg, _
    	keyData)
    -BoKu-

  6. #6
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: keypress problems

    An australian should be in bed right now ;p (i'm one to talk HAHA) thanks for the post, i hadn't heard of that before ;p

    I take it then, you have resolved the issue from ajays advice, perhaps marking this thread as resolved is in order then

  7. #7

    Thread Starter
    Hyperactive Member boku's Avatar
    Join Date
    Dec 2004
    Posts
    386

    Re: keypress problems

    nope still working on it... :P

    Gotta get up in 4 hours for work and i cba to stop working on a private project lol.
    -BoKu-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width