KeyDown Event - Convert If/End If blocks, to Select Case

I know this is basic so my apologies. I do understand the usage of Select Case but I can’t quite solve this issue in regards to pressing a key.

I’m drawing a simple xy graph in a canvas. This part is working fine. I’m able to pan/scroll/zoom using the keyboard arrow keys + a modifier key when required. What I’ve come up with is working ok but there is an issue with zoom, which after a few hours mucking around I’ve realised were the issue lies - I think.

It all happens in the Canvas KeyDown Event. Here’s the code:

[code] ‘----------’
If Keyboard.AsyncKeyDown(&h7B) then
scrollX = scrollX + (-scrollIncrementX) //Scroll LeftArrow
End If

‘----------’
If Keyboard.AsyncKeyDown(&h7C) then //Scroll RightArrow
scrollX = scrollX + (scrollIncrementX)
end if

‘----------’
If Keyboard.AsyncKeyDown(&h7E) then //Scroll UpArrow
scrollY = scrollY + (-scrollIncrementY)
end if

‘----------’
If Keyboard.AsyncKeyDown(&h7D) then //Scroll DownArrow
scrollY = scrollY + (scrollIncrementY)
end if

‘----------’
If keyboard.AsyncShiftKey AND Keyboard.AsyncKeyDown(&h7E) then //Shift+UpArrow ZOOM IN
graphScaleFactor = graphScaleFactor*2
end if

‘----------’
If keyboard.AsyncShiftKey AND Keyboard.AsyncKeyDown(&h7D) then //Shift+DownArrow ZOOM OUT
graphScaleFactor = graphScaleFactor/2
end if

ClearGraph
DrawGraph[/code]

I’ve used blocks of If/End If statements and now understand what’s happening. When I press the shift key for zoom, then press the up arrow, both events are evaluated. ie. Up Arrow first, Then Shift+UpArrow. In the old days I would have inserted a GOTO statement before each End If to break out… Oh dear :slight_smile:

I did originally consider using Select Case but I thought that it wouldn’t work with the boolean operator AND. As in this pseudo code:

myKeyPress$ = INKEY$ Select Case myKeyPress$ Case Keyboard.AsyncKeyDown(&h7E) Up Arrow, break out... Case keyboard.AsyncShiftKey AND Keyboard.AsyncKeyDown(&h7E) Shift+UpArrow, break out... End Select Now Methods/SubPrograms go here...

Sorry about the mixture of code syntax but hopefully it puts the point accross.

If it was just variables I could work it out, but using Keyboard.AsyncKeyDown is throwing me off a bit. This has become a sticking point and I can’t move on with the rest of the program until this is resolved.

Happy to hear any other ideas as well.

Cheers.

Hi Steve,

I haven’t tested this but I believe it should work:

Select Case True Case Keyboard.AsyncKeyDown(&h7E) Up Arrow, break out... Case keyboard.AsyncShiftKey AND Keyboard.AsyncKeyDown(&h7E) Shift+UpArrow, break out... End Select

HTH,

Julen

Select case is what I would do first.

This also works :

If Keyboard.AsyncKeyDown(&h7B) then scrollX = scrollX + (-scrollIncrementX) //Scroll LeftArrow ElseIf Keyboard.AsyncKeyDown(&h7C) then //Scroll RightArrow scrollX = scrollX + (scrollIncrementX) ElseIf Keyboard.AsyncKeyDown(&h7E) then //Scroll UpArrow scrollY = scrollY + (-scrollIncrementY) '.../... end if

Thanks Julen, I don’t understand. Are you actually saying that I can simply state “Select Case True” etc…? that seems so simple it’s embarrassing.

Thanks Michel for pointing out the If/ElseIf scenario. Like you said, workable but probably not as efficient as Select Case.

My original code was meant as “proof of concept” which did sort of work ok - but not correct. I haven’t worked on this program for about a week and trying to get back into it makes it more difficult. I write notes in a notebook of where I’m at, but even that becomes overwhelming sometimes - too much break in-between times.

I’ll test Julen’s method tomorrow and report back. It’s almost 11pm here and after a hard working week I’m now relaxing and having a few beers. Not the time for coding but time for thinking about aesthetics.

I’m looking forward to solving this. Cheers.

Hi Steve,

did you read docs Select_Case and/or developer.xojo.com/select-case ?

Yes Steve, that’s it.

Don’t be embarrassed, I learnt it recently from one of the forum gurus, and I would think, by the way he showed it, it is not such an obvious construction.

Julen

Indeed I believe Select case is way more elegant. But as often, there are several ways to get to the same result. That was all the sense of what I posted.

The Select Case True method works perfectly :slight_smile:
However, I did have to move the Zoom function to the beginning otherwise the arrow key was evaluated first resulting in unwanted scrolling and disabling of Zoom. This is the code I ended up with:

[code] Select Case True

//ZOOM-------------------------

Case keyboard.AsyncShiftKey AND Keyboard.AsyncKeyDown(&h7E) //Shift+UpArrow ZOOM IN
graphScaleFactor = graphScaleFactor*2

Case keyboard.AsyncShiftKey AND Keyboard.AsyncKeyDown(&h7D) //Shift+DownArrow ZOOM OUT
graphScaleFactor = graphScaleFactor/2

//SCROLL/PAN-------------------

Case Keyboard.AsyncKeyDown(&h7B)
scrollX = scrollX + (-scrollIncrementX) //Scroll LeftArrow

Case Keyboard.AsyncKeyDown(&h7C) //Scroll RightArrow
scrollX = scrollX + (scrollIncrementX)

Case Keyboard.AsyncKeyDown(&h7E) //Scroll UpArrow
scrollY = scrollY + (-scrollIncrementY)

Case Keyboard.AsyncKeyDown(&h7D) //Scroll DownArrow
scrollY = scrollY + (scrollIncrementY)

End Select

ClearGraph
DrawGraph[/code]

Thanks for all the help.

[quote=297935:@Emile Schwarz]Hi Steve,

did you read docs Select_Case and/or developer.xojo.com/select-case ?[/quote]

Sorry that I missed your post Emile. Yes, I did read the documentation but found it not very useful. I’ve learned much much more from this post, than any of the documentation provided - which is sometimes lacking in detail.