Basic: How is best way to test for the 'NUL' keycode (from Enter)?

How is the best way to test for the NUL returned by a key stroke event when the Enter key is struck?

When I enter some text in a list box control, and then type an Enter the Key_Pressed event fires and gives me oEvent. Contained in oEvent is KeyChar reporting the key that was typed, and in this case it’s set to NUL. (As reported by MRI).

But for some reason the Basic IsNull() (documentation is here) function (not the SQL IsNull() function) returns False rather than True as expected:

If( IsNull(oEvent.KeyChar) ) Then ...code to execute if CR found

As a workaround I’ve found this works, but it’s awkward and less obvious:

If oEvent.KeyChar Then
Else ...code to execute if CR found
End If

Is the NUL returned by KeyChar not the same as the Null tested for in the basic IsNull function?

BTW, every keystroke fires the Key_Pressedevent, so I’m just looking for when Enter is hit.

Also for some reason MRI calls this “NUL”, not Null.

Hello EasyTrieve, you could write:

If oEvent.KeyChar = chr(0) Then

alternatively you could also write:

If oEvent.KeyCode = 1280 Then

or even better:

 If oEvent.KeyCode = com.sun.star.awt.Key.RETURN Then

@librebel, Thanks, but but KeyChar is returning NUL. And by that I don’t mean the string "NUL" either. BTW, your idea is what I thought at first, but MRI reports what is actually being returned as NUL or an empty window depending on view.

one moment… I noticed that you suggested testing for KeyCode not KeyChar… testing it now

hmm i have Object Inspector, it shows an empty line for KeyChar when Return is pressed.

KeyCode = 1280 works, and it looks like NUL means chr(0), not Null; so I edited your answer for those that come to see this later. (BTW, I have no idea how to get it to use the RETURN constant, e.g. KeyCode = RETURN but there must be some way to get that constant to work.) Thank you!

You’re welcome EasyTrieve, and thank you for the Karma:)
instead of KeyCode = 1280 you could also write:

if oEvent.KeyCode = com.sun.star.awt.Key.RETURN Then

(see ref)

You beat me too it. After I just edited your A above to include this, I scrolled down to find your answer. :slight_smile: Thanks again.

@EasyTrieve KeyChar returns a character and KeyCode returns an integer.

Using xray the return key gives 0D hex for KeyChar (decimal 13 for CR) and 1280 for KeyCode.

The original posting by @librebel using -

If oEvent.KeyChar = chr(13) Then

or

If oEvent.KeyCode = 1280 Then

work for me. I don’t see why MRI gives NUL for the return key.

EDITED 24/04/2017

@EasyTrieve If you run the code below on KeyPressed event it will show the ascii code generated by all the character keys including Return as it is a character. Keys like the arrows or Function keys will return 0 as they are not characters and are used by the operating system.


Sub Press(oEvent)
Dim ky As Integer
ky = oEvent.KeyChar
MsgBox ky
End Sub

As confirmed by @librebel in his comment.

Thanks. I now think that when MRI says “NUL” it really means chr(0).

Yes i agree with @peterwt, the character chr(0) is NOT the Return character chr(13). The NUL character is used at the end of null-terminated C-strings. For object inspection i can recommend the Object Inspector extension ( “inspector.oxt” ).