Anyone knows why? was: Possible to substitute keys on entry in Xojo?

Hi all,

in my defense: it was a long day and I needed some distraction, so this may be a silly question but here it goes: is it possible to swap keys on entry?

Like in a TextField you press an “a” and a “w” appears instead?

I thought it should be easy, just reassign the key in the KeyDown or KeyApp event but that doesn’t work.

So I’m wondering how key replacement works, and if it is Xojo not making that functionality available, or what the reason for that is.

Any ideas beside mucking around with the TextChanged event?

KeyDown Event ?

Or more vicious: in the KeyDown Event, make a copy of the pressed key (as Window Property), then at KeyUp time, make a replace for that key. Easy for only one key, start to be complex for more pressed keys.

@Markus Winter — Intercept the key in KeyDown, insert what replacement you want with SelText="…" and return True from the event. And voila!

What Stephane said. I do this all the time.

if Key = "a" then
   me.SelText =  "w"
   return true
end

Thanks. Now that I had some sleep I got it too.

Lesson: go to bed when you have reached the point where thinking becomes difficult.

Just for completeness sake: the code has to be slightly different as the cursor is otherwise not where you expect it to be if you enter the key inside a text (tested on two Macs & one Windows):

If Key = "a" Then Dim selStart As Integer = Me.SelStart Me.SelText = "w" Me.SelStart = selStart + 1 Return True End If

Note that you also can’t do

Me.SelStart = Me.SelStart + 1

Not sure what is going on there as it seems somewhat screwy …

P.S. with

if Key = "a" then me.SelText = "w" return true end

if you enter an “a” inside the text then a “w” is correctly inserted, but the cursor always jumps to the end of the text.

And as I said adding Me.SelStart = Me.SelStart + 1 does not make a difference.

Anyone knows why???

[code]If key = “a” Then

Dim ss As Integer = Me.SelStart

Me.SelText = “w”

Me.SelLength = 0
Me.SelStart = ss + 1

Return True
End If[/code]

Isn’t that the same code I posted … :wink:

[quote=441650:@Markus Winter]Just for completeness sake: the code has to be slightly different as the cursor is otherwise not where you expect it to be if you enter the key inside a text (tested on two Macs & one Windows):

If Key = "a" Then Dim selStart As Integer = Me.SelStart Me.SelText = "w" Me.SelStart = selStart + 1 Return True End If
[/quote]

No because mine works :smiley:

Me.SelLength = 0

Mine works too … [where is the headscratching emoji?]

Or are you seeing something different?

Just doing

If Key = "a" Then Dim selStart As Integer = Me.SelStart Me.SelText = "w" Me.SelStart = selStart + 1 Return True End If

seemed to break on my mac, the caret would vanish and the text field would lose the ability to enter any more characters (other than a) so I added Me.SelLength = 0 which seemed to fix that problem on the mac.

I guess I found a bug and fixed it without you noticing it was an issue :wink:

I assume you mean cursor when you say caret?

I tried it now on 3 Macs, two with High Sierra, one with Mojave, all with Xojo 2018 R3. Adding SelLength = 0 makes no difference here.

Which System / Xojo combination are you using? As I think there might be a bug in Xojo anyway (those code contortions shouldn’t be necessary) it might well manifest in other combinations.

Have you tried restarting? Xojo also tends to become weird when it runs for weeks …

Maybe a difference between mac SDK 10.9 and 10.14?

No I mean Caret where da wordies go, not the pointies :slight_smile:

Yosemite 10.10.5 (Its a brand new VM, I just reinstalled and patched it to make sure)
Xojo 2019r1.1

https://www.dropbox.com/s/xwffj7el5kmexth/KeyReplacementCaret.mp4?dl=0

Caret. Got it. Thanks - learned a new word :slight_smile:

Yosemite or 2019R1.1 would explain it. Have to try out later which one it is.