We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question

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

asked 2017-04-21 21:00:06 +0200

EasyTrieve gravatar image

updated 2017-04-21 22:08:16 +0200

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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-10-01 13:37:26.785001

2 Answers

Sort by » oldest newest most voted

answered 2017-04-21 21:17:15 +0200

librebel gravatar image

updated 2017-04-22 01:12:41 +0200

EasyTrieve gravatar image

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
edit flag offensive delete link more


@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.

EasyTrieve gravatar imageEasyTrieve ( 2017-04-21 21:57:38 +0200 )edit

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

EasyTrieve gravatar imageEasyTrieve ( 2017-04-21 22:14:35 +0200 )edit

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

librebel gravatar imagelibrebel ( 2017-04-21 23:38:49 +0200 )edit

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!

EasyTrieve gravatar imageEasyTrieve ( 2017-04-22 00:43:02 +0200 )edit

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)

librebel gravatar imagelibrebel ( 2017-04-22 00:56:31 +0200 )edit

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

EasyTrieve gravatar imageEasyTrieve ( 2017-04-22 01:14:04 +0200 )edit

answered 2017-04-23 00:09:12 +0200

peterwt gravatar image

updated 2017-04-24 16:01:24 +0200

@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


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.

edit flag offensive delete link more


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

EasyTrieve gravatar imageEasyTrieve ( 2017-04-23 05:31:51 +0200 )edit

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" ).

librebel gravatar imagelibrebel ( 2017-04-23 13:27:28 +0200 )edit

Question Tools

1 follower


Asked: 2017-04-21 21:00:06 +0200

Seen: 221 times

Last updated: Apr 24 '17