B4J Question SOLVED - Any way to know if a particular key is currently pressed?

Mikelgiles

Active Member
Licensed User
Longtime User
I need to know if the ALT ( or other) key is depressed while working on a B4XTable grid. I just need to know if the key is being depressed at any given time, or if it was being depressed when a mouse click happens, or at the same time as a B4XTable_CellClicked(or longclicked). I see a eventdata altdown but have not figured out how to use it(not available in B4J?).

There is certainly a lot more power in this language than I accustomed to and the longer I work with it the more I am understanding that I don't know. I am wondering if I should spend some time learning java and how much help that would be.
 

sorex

Expert
Licensed User
Longtime User
when you add the GameView library you have the ability to read out keyboard presses.

If it is suitable for what you want to do that's up to you to test it.

In theory if you set a global var during keypress and erase it when the keys are release you should get what you want.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This project has a little bit of inline java that works with the JNA jar's which are downloadable from here https://github.com/java-native-access/jna. It currently checks for the Ctrl key. Alt key is 0x12.

1581038231302.png


Click the buttons then the download link top right of the page that opens, not the text link. And save them in your additional Libraries folder.

The code was taken from this github project https://github.com/Col-E/Simplified-JNA/blob/master/src/me/coley/simplejna/Keyboard.java

You need to know the Virtual key code for the keys you want which are available here : https://docs.microsoft.com/en-us/op.../ms-tvtt/261ddfb0-ce10-4380-9b7a-4b50f482b8ec

The library jars are quite large, but the method works and is queryable, rather than event driven if you don't find another way.
 

Attachments

  • KeyPressTest.zip
    2.2 KB · Views: 149
Last edited:
Upvote 0

Mikelgiles

Active Member
Licensed User
Longtime User
when you add the GameView library you have the ability to read out keyboard presses.

If it is suitable for what you want to do that's up to you to test it.

In theory if you set a global var during keypress and erase it when the keys are release you should get what you want.
Thanks to sorex this problem is solved and works perfect. Thank you sorex!!!

I added the library GameViewHelper, added code in 3 places to get the function setup amd in one place where I needed to know if the Shift key was depressed. The same thing could be done for other keys such as "Alt" or "Ctrl" with very little code added.

Add library GameViewHelper

B4J code:
'Process_Globals
    Public GameViewHelper As GameViewHelper
    Public KeyAlt, KeyShift,KeyCtrl As Boolean

'AppStart
    GameViewHelper.AddKeyListener("GVH",MainForm)   ', Main.MainForm)

'Main Module

Sub GVH_KeyPressed (KeyCode As String) As Boolean
    If KeyCode =  "Shift" Then KeyShift = True '"Alt" & "Ctrl" also work
End Sub
Sub GVH_KeyReleased(Keycode As String) As Boolean
    If Keycode =  "Shift" Then KeyShift = False
End Sub

Sub B4XTable_CellClicked
        If KeyShift = True Then Return
 
Upvote 0
Top