searchField and keydown etc

  1. As far as I can see (macOSlib and Jim’s searchField), a searchField doesnt have a keydown event. Yet the searchField I need must be able to filter keys and allow only a predifined set of keys. So I tried the following:

//get the last chr of the stringvalue
dim t as String = Right(me.StringValue, 1)

//fireaction when Return key has been pressed: cannot have it work
if t = chr(13) or t = chr(11) then
//do something
me.stringValue = “”
Return
end if

//allow bangla characters only
if t = chr(0) or instr(kBanglaGlyphs, t) > 0 or t = chr(8) then
//it works OK
Return
else
me.StringValue = “”
end if

Therefore: how to detect a Return?

  1. Is it possible to set textfont and textsize?
    in the open event (macOSlib > searchField) I put
    me.textfont = “myBanglaFont”
    me.textSize = 18
    me.height = 24

Yet the font doesnt change, nor its size, nor the size of the field.
So I used a Xojo textField with bezeled set to true (macOSlib > textField); but once bezeled, this textField too doesnt honor textFont, textSize and height; probably because it has been “transformed” into a NSTextField.

Any idea how to bypass such restrictions? Thanks

I know that I created a custom NSSearchField class at one point similar to the one Jim made and the one in MacOSLib and I was able to add a TextChanged event (that event is provided by the OS) but you are correct that there are no keydown/pressed/up/etc events available for an NSSearchField. What you will need to do is cache the text value with a variable and then compare the new value with the old value to determine what key was pressed.

Hopefully this helps,
Jason

Yes, that I did. But as I said, I cant trap endoflines (chr(13) or chr(11).

Maybe you could use Keyboard.AsyncKeyDown to detect enter and whatever key combination that does chr(11) ?

the following code in a timer does not return “YES”
if Keyboard.AsyncKeyDown(&h4C) then//Enter key
msgbox “Yes”
end if

yet the following code returns “YES”
if Keyboard.keyname(&h4C) = “Enter” then
msgbox “Yes”
end if

So I cant understand why the first snippet does not work.
On the other end, the fact that a searchfield’s textfont, textSize and Hight cannot be set by code, precludes my usage of it.
Thanks.

[quote=203175:@Carlo Rubini]the following code in a timer does not return “YES”
if Keyboard.AsyncKeyDown(&h4C) then//Enter key
msgbox “Yes”
end if

yet the following code returns “YES”
if Keyboard.keyname(&h4C) = “Enter” then
msgbox “Yes”
end if

So I cant understand why the first snippet does not work.
[/quote]

You may want to try 24.

40254 - Regular Return missing in the LR for Keyboard.Keydown
Status: Needs Review Rank: Not Ranked Product: Xojo Category: N/A

Michel Bujardet Today at 1:18 PM
OS: OS X 10.11.0

Xojo: Xojo 2015r2.2

Steps: In the LR at Page Not Found — Xojo documentation in the list of special keys, Enter is missing.

The Enter key of the typewriter keyboardhas a value of Hex 24.

The one mentioned in the numeric keypad has a different value of 4C, which is inadequate.

<https://xojo.com/issue/40254>

That’s why I could not proceed.
Now, with Hex 24 things work OK. Not only, but the code works also in the action event of the searchField, that is, no timer needed.
Thanks.

For textfont and textsize, create a rounded Textfield using the declare posted here https://forum.xojo.com/6332-rounded-textfield/0

And use the magnifying glass emoji as cue text.

Sorry, haven’t tried it, but a NSSearchField inherits from NSView which knows a keydown event. In my opinion it should be possible to implement one. Wouldn’t that be much easier?

Unfortunatelly, as I pointed out in my first post, once a TextField get bezeled either throu macOSlib or the forum reference above, it seems that its height cannot be set.
So, once I set fontSize to 18, half of the glyphs are no more visibles (at least with Bengali fonts).
(Although I was wrong in the same post when I said that textFont and textSize cannot be set; in fact they can be set).

[quote=203190:@Carlo Rubini]Unfortunatelly, as I pointed out in my first post, once a TextField get bezeled either throu macOSlib or the forum reference above, it seems that its height cannot be set.
So, once I set fontSize to 18, half of the glyphs are no more visibles (at least with Bengali fonts).
(Although I was wrong in the same post when I said that textFont and textSize cannot be set; in fact they can be set).[/quote]

Indeed that is one limitation for some button styles as well I discovered with RubberViews. There is no way around it, as far as I know.

Seems the only way now is to use a canvas.

At present my workaround would be to place a lens-icon in front of a normal textfield, although it doesnt look as expected.
I tried also putting the icon inside the textfield, but then the text overlaps the icon; and inserting a couple of spaces at the beginning of the field creates other problems with my code.
Eventually I’ll leave things as they are, i.e. no icon.

It could have to do with this:

https://books.google.de/books?id=wLaVBQAAQBAJ&pg=PT637&lpg=PT637&dq=ios+round+style+textfield&source=bl&ots=7ttC4LY2DU&sig=B5GX7aPJ8VbZ2FiI0PxjeqEv5VI&hl=de&sa=X&ved=0CDIQ6AEwAjgKahUKEwjK9ubNr-jGAhXFXRQKHQurAxg#v=onepage&q=ios%20round%20style%20textfield&f=false

I am not sure, but it could be that sizetoFit is called, returns a wrong size and therefore the textfield cuts the text. Maybe overriding sizetoFit could help?

Only if I knew how to do it…

If you are using a declared version of a searchfield, could you post it? sizeToFit (and Keydown/KeyUp) would have to be implemented on the custom class. Depends a bit on how your implementation is set up.

Here is the macOSlib code I have in the open event of the textfield: the commented one is the one posted by Sam.

#if TargetMacOS
me.Bezeled = true
me.BezelStyle = NSTextFieldBezelStyle.NSTextFieldRoundedBezel
'declare sub setBezelStyle lib “Cocoa” selector “setBezelStyle:” ( handle as integer, value as integer )
'setBezelStyle( me.handle, 1 )
me.TextFont = SomeFont
me.TextSize = 18
me.Height = 22
#endif

May be you were expecting also the code in the keydown event of the declared textfield, which is:

if key = chr(13) or key = chr(11) then
if me.text = “” then
//do Something
else
//do something else
end if
Return true
end if

if key = chr(0) or instr(kBanglaGlyphs, key) > 0 or key = chr(8) then
//allow bangla glyphs
Return false
end if

return true

[quote=203193:@Carlo Rubini]At present my workaround would be to place a lens-icon in front of a normal textfield, although it doesnt look as expected.
I tried also putting the icon inside the textfield, but then the text overlaps the icon; and inserting a couple of spaces at the beginning of the field creates other problems with my code.
Eventually I’ll leave things as they are, i.e. no icon.[/quote]

The emoji in the cue text works quite well, you know.

Yes, I found it. Thanks.