The documentation (Trapping the Arrow Keys)

I needed to trap/execute action depending on the pressed Arrow Key in Canvas1.MouseDown.

I wasted my time (without Internet for while) because I had a problem with my code and I was not sure about the arrow values on the keyboard/Canvas.KeyDown(Key).

The used code is:


// Read the image number #…
If key = Chr(28) And Img_Nbr > 1 Then // Left Arrow
  Img_Nbr = Img_Nbr - 1
ElseIf key = Chr(29) And Img_Nbr < Img_Cnt Then // Right Arrow
  Img_Nbr = Img_Nbr + 1
ElseIf key = Chr(30) Then // Up Arrow
  Img_Nbr = 1
ElseIf key = Chr(31) Then // Down Arrow
  Img_Nbr = Img_Cnt
Else
  System.DebugLog "Unknow Key"  // *
End If

I rarely get the Left Arrow fired, but the key works fine elsewhere.

Is there’s something wrong ?

  • I was not able to transform Key to a decimal value… tired !

Do you think the above code (if correct) must be set back in the documentation (instead of nth times the same other code) ?
Other code to deal with the Arrow Keys (Keyboard.AsyncKeyDown(123) to &26)

have you other controls where KeyDown return true?
does this canvas not have the focus?

have you set Allow Focus in Canvas Inspector Settings?

is Img_Nbr a computed propertie?

No Return True
Yes, it have the Focus
Yes Allow Focus and Allow Tab
No, Img_Nbr is not a Computed property

I removed them, but at one time, I had in each If & ElseIf entry:
System.DebugLog "[28] Left Arrow"
with the appropriate text.

Old monkey here.

I find it easier to use:

select case ask( Key )
   case Key_Left
   case Key_Right
   // etc
end select

My key constants are as follows:

Private Const KEY_BACKSPACE as Number = 8
Private Const KEY_DOWNARROW as Number = 31
Private Const KEY_ESCAPE as Number = 27
Private Const KEY_LEFTARROW as Number = 28
Private Const KEY_PAGEDOWN as Number = 12
Private Const KEY_PAGEUP as Number = 11
Private Const KEY_RETURN as Number = 13
Private Const KEY_RIGHTARROW as Number = 29
Private Const KEY_TAB as Number = 9
Private Const KEY_UPARROW as Number = 30

If you are acting on the key then you should “Return True” from the event, otherwise other elements start reacting to keys.

2 Likes

I find it easier to use:
You are right. But at 7 in the morning of a white night, one can took the other way.

Thank you professor. But System.DebugLog display nothing for the left arrow ley press. That means it # is ignored (or wrong) or something else (what ?) happened.
That is the object of my question.

Maybe Img_Nbr is not > 1 when you’re pressing left arrow? To fully debug, you need to separate the conditions.

1 Like

its maybe 0 as default value and not 1.
which means you can press right it get 1 but at left it is not >1.

I started the If block testing only Key. I added the second condition later, to avoid asking a file that does not exists: I use aPict = Picture.Open(Img_FI.Item(Img_Nbr)) to load the image file. The FolderItem is a Reference to a Folder with images.

The testing folders have > 20 and > 200 files each, and:
Img_Nbr is declared as Static, init at 1 (else I get a crash).

Apparently Left Arrow is Chr(28) and have to work in the IDE.

I even clear the Xojo Cache.

Old Horse, I told you ( for the record, I started with REALbasic in may 1998…ß…) and buy version 1 sometimes later when it was available.
And, yes, I can make mistakes

to find everything
in the events i use System.DebugLog CurrentMethodName
and track any relevant in and out with System.DebugLog

if you make Img_Nbr as Computed property you can track any change there.

Img_Nbr is declared as Static, init at 1

Static is usually used in local function scope.
i thought it is a normal propertie because you u used it also at KeyDown.

this index is also 1 to count? (and not 0 to count-1)

Img_FI.Item(Img_Nbr)

I just tried a simple test here and all arrow keys are correctly detected in Canvas.KeyDown.

Maybe your keyboard has a bad key? :slight_smile:

I’ve done all of that.

A bad key, sometimes (sometimes more bad than some other times), I am starting to think.

BTW: I used KeyUp. I copied the code to KeyDown an hour ago and set the image resize (I keep the W/H ratio) correctly (it was time). I need to Sort set the files in an Array and Sort them, so I can see them “in order”… (1…200 or by dates depend on the test files).

Thank you all.

My previous MBP had something like four keyboard replacements under AppleCare for stuck/non-working keys. My current 2022 model is much much better but occasionally has issues that seem to fix themselves over time. Concidentally, I think left-arrow was the most recent one to go flaky, but now it’s working :woman_shrugging:

should obviously be:

select case asc( Key )

I’ve had it work on Windows and Mac, I’m not sure about Linux.

At first I thought it was some new API2 function I wasn’t familiar with, then I realized… :smiley:

2 Likes

It’s what I get for typing things on an iPhone :slight_smile:

I knew Speak(), but why not ! :wink:

1 Like

Sometimes the Command-Key is ignored and I get a c or p instead of Copy or Paste :frowning:
So, maybe the MBP m1 (from 2020 ?) keyboard needs a replacement…