1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4J Question Can a Keyboard event be detected

Discussion in 'B4J Questions' started by BPak, Feb 27, 2018.

  1. BPak

    BPak Active Member Licensed User

    Check for KeyPressed?
    Is there a method of checking if the 'Ctrl' Key (and other keys) are Pressed when clicking on a Canvas?
  2. stevel05

    stevel05 Expert Licensed User

    Try this:
    Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Canvas1 As Canvas
    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
    'Layout only holds Canvas1
        MainForm.RootPane.LoadLayout("1"'Load the layout file.
    Dim CJO As JavaObject = Canvas1
    Dim O As Object = CJO.CreateEventFromUI("javafx.event.EventHandler","CanvKeyPressed",Null)
    End Sub

    'Return true to allow the default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
    Return True
    End Sub

    Sub CanvKeyPressed_Event (MethodName As String, Args() As Object) As Object
    Dim KEvt As JavaObject = Args(0)
    End Sub
    The loaded layout only holds canvas1.
    xulihang, Tayfur and BPak like this.
  3. BPak

    BPak Active Member Licensed User

    Thanks Steve.
    It works fine. Just have to make sure the Canvas has focus and it works excellent.

    Is there a way of detecting KeyDown, KeyUp with your method?
  4. Daestrum

    Daestrum Well-Known Member Licensed User

    setOnKeyPressed is keydown
    setOnKeyReleased is keyup

    so you could just add another handler for the setOnKeyReleased event

    (When I used similar code, it actually stops the canvas.mouseclicked event from firing so I had to add a handler for that too.)
    BPak and stevel05 like this.
  5. Roycefer

    Roycefer Well-Known Member Licensed User

    BPak likes this.
  6. BPak

    BPak Active Member Licensed User

    Yes, that could do what I am looking at...
  7. BPak

    BPak Active Member Licensed User

    I found with the sample program using Steve's example that the Focus would go to a Button I had on the Window after using KeyPressed = "UP" therefor no key press was detected by the canvas. So I redirect the focus to the Canvas and it works as expected.

    Will have a look at the NativeHook. thank you.
  8. BPak

    BPak Active Member Licensed User

    Thanks for the JavaObject code...
    Works as required when wanting to draw a Horizontal or Vertical Line.

    Code attached for anyone interested.

    Attached Files:

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice