Author Topic: Add keystroke (shortcut) to basic button  (Read 453 times)

authorleon

  • Newbie
  • *
  • Posts: 8
    • View Profile
Add keystroke (shortcut) to basic button
« on: May 22, 2017, 10:50:05 AM »
Hello,

I am trying to make a simple application and I have some buttons that work with the AB SLC500. Great.

I would like to add on the button a key shortcut so the user can press the button or press a key.

Code: [Select]
  Private Sub BasicButton1_Click(sender As Object, e As EventArgs) Handles BasicButton1.Click

    End Sub

I have been trying for about 6 hours but no joy. The code above is what I have as the standard code.

Thank you very much.

Godra

  • Hero Member
  • *****
  • Posts: 566
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #1 on: May 22, 2017, 12:18:20 PM »
To assign a shortcut one could possibly try using the MainForm's "KeyUp" event:

Code: [Select]
    Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        If e.Alt AndAlso e.KeyCode = Keys.B Then
            BasicButton1.PerformClick()
        End If
    End Sub

This is a general approach and should not work properly with the BasicButton control since this control relies on the MouseDown and MouseUp events to perform its function. It should work with whatever code you might include in the sub you posted.
« Last Edit: May 22, 2017, 12:28:09 PM by Godra »

authorleon

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #2 on: May 22, 2017, 05:03:14 PM »
Hello,
Thank you for the info, but it did not work:

Code: [Select]
Public Class MainForm

    Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        If e.Alt AndAlso e.KeyCode = Keys.W Then
            BasicButton1.PerformClick()
        End If
    End Sub
    '*******************************************************************************
    '* Stop polling when the form is not visible in order to reduce communications
    '* Copy this section of code to every new form created
    '*******************************************************************************
    Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        AdvancedHMIDrivers.Utilities.StopComsOnHidden(components, Me)
    End Sub

    '**************************************
    '* Filling the form with a gradient
    '**************************************
    'Private Sub MainForm_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    'Dim rect As New System.Drawing.Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height)
    'Dim gradientBrush As New Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Height), System.Drawing.Color.FromArgb(180, 100, 200), System.Drawing.Color.FromArgb(110, 200, 255))
    'e.Graphics.FillRectangle(gradientBrush, rect)
    'End Sub

    Private Sub BasicButton1_Click(sender As Object, e As EventArgs) Handles BasicButton1.Click
     
    End Sub

    Private Sub BasicButton7_Click(sender As Object, e As EventArgs) Handles BasicButton7.Click

    End Sub

    Private Sub BasicButton2_Click(sender As Object, e As EventArgs) Handles BasicButton2.Click

    End Sub

I have posted all the code I have at present.

Thank you very much.

If there is a better way. I would like to know. I am energizing: O:9/3.

When I press the button it energizes: O:9/3. When I let go, it stop.

Thank you
« Last Edit: May 22, 2017, 05:05:20 PM by authorleon »

Godra

  • Hero Member
  • *****
  • Posts: 566
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #3 on: May 22, 2017, 06:58:15 PM »
I did suggest that this would not work with the BasicButton because its functionality depends on the mouse events.

You could possibly simulate the functionality by writing the values with the driver, similar to this:

Code: [Select]
    Private Sub MainForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.Alt AndAlso e.KeyCode = Keys.W Then
            ModbusTCPCom1.Write("00001", "1")
        End If
    End Sub

    Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        If e.Alt AndAlso e.KeyCode = Keys.W Then
            ModbusTCPCom1.Write("00001", "0")
        End If
    End Sub

Instead of ModbusTCPCom1 you would use your driver and instead of "00001" you would use your address O:9/3.

I am not sure if it is a good practice to energize outputs of the PLC outside of the ladder program (but if it works for you then it works for me as well).

authorleon

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #4 on: May 30, 2017, 07:59:17 AM »
Hello,
I did what you said but i get this error.

Please have a look at the image attached. Also, I am using a serial connection.

Thank you


authorleon

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #5 on: May 30, 2017, 08:09:59 AM »
Hello, I fixed the errors but the bode does not work. I have tried.

Please let me know when you can.

Also, I do not need a person to press ALT+W... Just W directly when focused on the window.


Thank you
« Last Edit: May 30, 2017, 08:11:55 AM by authorleon »

authorleon

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #6 on: May 30, 2017, 09:45:16 AM »
After a bit of testing, it seems the form does not respond to any key presses.

Sorry, have I missed something.

authorleon

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #7 on: May 30, 2017, 10:03:40 AM »
Go it working.

Please note the following:

https://www.codeproject.com/Questions/376421/Csharp-Keydown-or-keycode-not-working

he reason is that when a key is pressed it will go to the control which has focus on the form, as the KeyPreview property of Form is set to False by default. Let us say the cursor is in a TextBox. Now, if the F3 key is pressed it will not go to the Form's KeyDown event and instead it is captured by the TextBox and the KeyDown event of TextBox fires.

So, to enable the KeyDown event of Form even when the focus is on any other control, then the KeyPreview property of the Form has to be set to true as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx[^]
KeyPreview Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
and under remarks as
To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true.



Godra

  • Hero Member
  • *****
  • Posts: 566
    • View Profile
Re: Add keystroke (shortcut) to basic button
« Reply #8 on: May 30, 2017, 08:19:57 PM »
You should be careful with these since your wish is to use letter W only.

If you set the KeyPreview to True and a letter W needs to be typed into any TextBox then you will get both processed (your code as well as the content of the TextBox).

The Alt+W combination is not normally typed into a TextBox.