B4J Question B4J JavaObject Oracle KeyPressed/KeyReleased Event Sub?

RonC.

Member
Licensed User
Longtime 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:


B4X:
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:

B4X:
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!
 

Roycefer

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…