locked
How can I handle the key pressed events RRS feed

  • Question

  • Hi MSDN

    I've been given a task at school to do the ATM application and now my main problem is to handle the three ATM buttons which are as follows

    1) Cancel

    2) Clear

    3) Enter

    I've done the rest just stuck in these button the main problem for me is that I've never handled any other keyboard keys other than the QWERTY keyboard which is much easier then handling the ATM keyboard keys. I'm not sure if something like this will work or not.

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            If Keys.Cancel Then
                Me.Close()
            Else
                If Keys.Clear Then
                    ' textbox.text = ""
                Else
                    If Keys.Enter Then
                        ' Do the desired task here
                    End If
                End If
            End If
        End Sub

    and also my main problem is withdrawing so under the keys.Enter it must perform the withdrawal after pressing enter in the ATM keyboard then the withdrawal must be performed.

    Your help will be appreciable.thank you

    Wednesday, May 15, 2013 10:47 AM

Answers

  • You forgot to mention that perhaps the user has used VB6 BUT who has 30 years of experience prior to VB6 as a development engineer. So VB6 is relatively brief.

    Actually we used to used command procedures to set up an envronment.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Saturday, May 18, 2013 2:08 PM

All replies

  • Hi,

     You should turn on option strict and option explicit in the VB options or turn it on by adding this as the very first 2 lines in your code.

    Option Strict On
    Option Explicit On
    

    This will point out some errors in your code by placing little blue lines under the errors. After that you may want to use the Form_KeyDown event like this

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.Cancel Then
                Me.Close()
            ElseIf e.KeyCode = Keys.Clear Then
                TextBox1.Clear()
            ElseIf e.KeyCode = Keys.Enter Then
                'Do whatever code is needed here
            End If
        End Sub
    
    End Class
    

    Wednesday, May 15, 2013 11:20 AM
  • Hi MSDN

    I've been given a task at school to do the ATM application and now my main problem is to handle the three ATM buttons which are as follows

    1) Cancel

    2) Clear

    3) Enter

    I've done the rest just stuck in these button the main problem for me is that I've never handled any other keyboard keys other than the QWERTY keyboard which is much easier then handling the ATM keyboard keys. I'm not sure if something like this will work or not.

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            If Keys.Cancel Then
                Me.Close()
            Else
                If Keys.Clear Then
                    ' textbox.text = ""
                Else
                    If Keys.Enter Then
                        ' Do the desired task here
                    End If
                End If
            End If
        End Sub

    and also my main problem is withdrawing so under the keys.Enter it must perform the withdrawal after pressing enter in the ATM keyboard then the withdrawal must be performed.

    Your help will be appreciable.thank you


    If you've done the rest and that works then what are you having a problem with?

    You've taught me everything I know but not everything you know.

    Thursday, May 16, 2013 5:21 AM
  • Hi MSDN

    I've been given a task at school to do the ATM application and now my main problem is to handle the three ATM buttons which are as follows

    1) Cancel

    2) Clear

    3) Enter

    I've done the rest just stuck in these button the main problem for me is that I've never handled any other keyboard keys other than the QWERTY keyboard which is much easier then handling the ATM keyboard keys. I'm not sure if something like this will work or not.

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            If Keys.Cancel Then
                Me.Close()
            Else
                If Keys.Clear Then
                    ' textbox.text = ""
                Else
                    If Keys.Enter Then
                        ' Do the desired task here
                    End If
                End If
            End If
        End Sub

    and also my main problem is withdrawing so under the keys.Enter it must perform the withdrawal after pressing enter in the ATM keyboard then the withdrawal must be performed.

    Your help will be appreciable.thank you


    If you've done the rest and that works then what are you having a problem with?

    You've taught me everything I know but not everything you know.

    Well my main problem is how can I call the withdrawal function something like this :

    If Keys.Enter Then
    If Textbox1.text = "" Then
    label1.text = "Please enter the amount first"
    Else
    withdrawal.Equals(Textbox1.text) = True ' Note that this is just an example so here I want to fire the withdrawal event, so that where my main problem is currently.
    me.close()
    End If
    End If

    that how I want it.

    Thank you

    Thursday, May 16, 2013 1:21 PM
  • IronRazerz

    Thank you for your help there, so my question now for you is that will these codes work on an ATM because my main concern is that the ATM is not using the QWERTY keyboard they use their own special keyboards, thank you so much.

    Thursday, May 16, 2013 1:24 PM
  • IronRazerz

    Thank you for your help there, so my question now for you is that will these codes work on an ATM because my main concern is that the ATM is not using the QWERTY keyboard they use their own special keyboards, thank you so much.


    I have to admit that don`t really know. I never used an ATM keyboard so i don`t know how they work. Sorry i can not help with that.  :(
    Thursday, May 16, 2013 1:44 PM
  • You really need to ask the hardware manufacturer.  Are you able to detect any keypress events?

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/


    • Edited by Rudedog2 Thursday, May 16, 2013 2:42 PM
    Thursday, May 16, 2013 2:41 PM
  • With these codes that I was given by IronRazerz what happens is that it just detect any key so I have 2 ideas in which I think I can manage to solve this problem but my problem is that I don't even know where I will start because I've never worked with things like this before.

    1) I think if I will use the method to get the first inserted or plugged device and from that device detected then set to detect if it Cancel, Clear, Enter buttons is being pressed or not. and on this method the problem is that I've never worked with first inserted device method so I don't even know how I will start doing so. My second Idea

    2) Perhaps if I will just set it to only detect if the Cancel button is being pressed 5 times with no pause longer than 5 seconds apart so the problem is adding it to only detect if the cancel button is pressed 5 times.

    Ow and I just remembered something which I noticed when I was trying to make a cellphone app was that you can call something like 

    DeviceKeys
    Which I saw something in my vb which goes to something more like that but I don't know how I will call it to detect the device keys pressed something like this 
    If Devices.Keyboard = Keys.Enter Then
    
    End If

    Saturday, May 18, 2013 10:28 AM
  • Hi,

     If the code i gave you is detecting that a key was pressed on your ATM keyboard then maybe you could find the key values that the ATM keyboard is sending. Just for the heck of it you could try making a new project and add 1 textbox to the form and use the code below. Press one of the keys on the ATM keyboard real quick and see what kind of ascii values are in the textbox. For example if i press (1) then (5) then (Esc) it will show (49  53  27) in the textbox which are the ascii values for the keys. If you are not getting the same values for all three of the keys on your ATM keyboard then you could detect the keys pressed by the ascii values. You will need to clear the textbox text before pressing each of the keys. If this does not work then i would try what Rudedog2 recommended to find a website for the keyboard manufacturer and see if they have an API that you can download that is made for the keyboard.  :)

    Public Class Form1
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            TextBox1.Text &= e.KeyValue.ToString & "  "
            e.SuppressKeyPress = True
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.KeyPreview = True
        End Sub
    End Class
    

    Saturday, May 18, 2013 12:26 PM
  • "You should turn on option strict"

    I would totally disagree with this.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Saturday, May 18, 2013 1:14 PM
  • "You should turn on option strict"

    I would totally disagree with this.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me


    This is a common recommendation.  Please backup your opinion with some reasoning or withdraw the comment.  As it stands this could be interpreted as flaming and adds no value to the thread.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 18, 2013 1:26 PM
  • OK I will. Compilers [should] come with workable defaults. The option changes the defaults.

    Actually I have never used an option because I was an engineer for the company who was #2 before MS. It maybe a common recommendation for those people who dont know any better.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me




    Saturday, May 18, 2013 1:30 PM
  • "You should turn on option strict"

    I would totally disagree with this.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Hi Renee,

     I am not going to argue with you about this but, instead of just saying that you "totally disagree with this" can you give a real good explination or example of why it should not be turned on in this case? I know from prior threads that you don`t like to use it but, i know a lot of people who like using it including myself and i have never had a problem with it. If anything it has helped me a few times.  :)

    Saturday, May 18, 2013 1:30 PM
  • IronRazerz

    Thank you for your help there, so my question now for you is that will these codes work on an ATM because my main concern is that the ATM is not using the QWERTY keyboard they use their own special keyboards, thank you so much.


    I have to admit that don`t really know. I never used an ATM keyboard so i don`t know how they work. Sorry i can not help with that.  :(

    If the device is a standard keyboard then any custom keys must be mapped to an existing key or key combination.

    Please try pasting this code into a new application, then press each key on the keypad and see what values are displayed:

    Public Class Form1
        Friend KeyInfoListBox As New ListBox With {.Dock = DockStyle.Fill}
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.KeyPreview = True
            Controls.Add(KeyInfoListBox)
        End Sub
    
        Protected Overrides Function IsInputKey(keyData As System.Windows.Forms.Keys) As Boolean
            Return True
        End Function
    
        Protected Overrides Sub OnKeyUp(e As System.Windows.Forms.KeyEventArgs)
            MyBase.OnKeyUp(e)
            KeyInfoListBox.Items.Add(e.KeyData.ToString)
        End Sub
    End Class

    This is the same idea as IronRazerz, but you need to allow all keys as input and should examine the KeyData to see any combinations of modifier keys that might be used.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 18, 2013 1:34 PM
  • It has also helped me once or twice but it's generally not worth the additional effort. One must consider that an option changes a compiler's defaults. Why weren't the options put there as a default if it's a good idea?

    It a good option for novices,

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me


    Saturday, May 18, 2013 1:45 PM
  • It has also helped me once or twice but it's generally not worth the additional effort. One must consider that an option changes a compiler's defaults. Why weren't the options put there as a default if it's a good idea?

    It a good option for novices,

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me



    The default is in place due to perceived expectations of developers coming from past versions of VB.  Once a developer is comfortable in VB.Net and has lost most of their VB6 (or prior) habits, it is best to change the default option to On.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 18, 2013 1:58 PM
  • You forgot to mention that perhaps the user has used VB6 BUT who has 30 years of experience prior to VB6 as a development engineer. So VB6 is relatively brief.

    Actually we used to used command procedures to set up an envronment.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Saturday, May 18, 2013 2:08 PM