B4J Question B4J JavaObject Oracle KeyPressed/KeyReleased Event Sub?

Discussion in 'B4J Questions' started by RonC., Aug 16, 2015.

  1. RonC.

    RonC. Member Licensed User

    I’m working on a final project for ENGR 270 class which is due in two weeks. It’s a Microchip PIC18F1220 Calculator, which takes in input from either a Binary TextField, Hex TextField or a Decimal Hexfield. I was able to filter out my input, so that decimal field only takes in a number the following way:


    Code:
    Sub Decimaltxtfld_TextChanged (Old As String, New As String)
      
    If IsNumber(Decimaltxtfld.Text) Then
         Decimaltxtfld.Text = New
      
    Else
         Decimaltxtfld.Text = Old
      
    End If
    End Sub
    However, when it comes to the Binary Text Field or the Decimal Hex field, the results don’t come out as well:

    Code:
    Sub Binarytxtfld_TextChanged (Old As String, New As String)
      
    If Binarytxtfld.Text.StartsWith("0"Or Binarytxtfld.Text.StartsWith("1"Then
         Binarytxtfld.Text = New
      
    Else If Binarytxtfld.Text.Length > 0 Then
         Binarytxtfld.Text = Old
      
    Else
         Binarytxtfld.Text = 
    ""
      
    End If
    End Sub
    The problem with this code is that it only checks the end of the string, so it’s possible to put erroneous input into this code.

    I have no knowledge of Java, but I believe that the only solution would be to determine which field is being typed to, setting an event that filters out the characters that are not wanted. (ie: binary = only 0 or 1, Hex 0-9,A-F,a-f…) instead of calling a function and scanning the entire string every time a key is pressed. I believe this would trap the event before the TextChanged event would happen…

    I’ve looked into the JavaObject code, but I’m having difficulty deciphering the code. Most JavaObjects refer to Android Code and not Oracle Code. I’ve looked at the chart example by steve05, but I don’t see how that code could be used to create an Event Sub in my code.

    I believe this is what I’m looking for: https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

    While an answer would be great, understanding would be even better in case I need to create new events while building this project. If anyone knows of a quick way to learn java structures (Oracle) with code examples (in Java), that would be great too. :) But right now, understanding how to integrate some sort of keystroke event accepting keys depending on the focus on a textfield is paramount!
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Just go over all the characters in the string. It will take less than 1ms.
    Code:
    For i = 0 To Binarytxtfld.Text.Length - 1
     
    Dim c As Char = Binarytxtfld.Text.CharAt(i)
     ...
    Next
     
    RonC. likes this.
  3. Roycefer

    Roycefer Well-Known Member Licensed User

    If you want to trap keyboard input events before they are propagated to their "owner" application, take a look at the jNativeHookB4J library. However, I think this is a kludgey solution to your problem.

    I think you should just copy your pattern from the Decimaltxtfld example. Implement your own isBinary() and isHex() methods using the loop Erel showed you.
     
    RonC. likes this.
Loading...
  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