473,837 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch keys vs if else if

hi
i just discovered something strange

when i use in ProcessCmdKey
this construction

switch(keyData)
{
case Keys.Left:
{
...
}break;
case Keys.Right:
{
...
}break;
}

everuthing is fine
but in this construction

if((keyData & Keys.Left) == Keys.Left)
{
...
}
else if ((keyData & Keys.Right) == Keys.Right)
{
...
}

program goes into "left" no matter if i press left or right arrow
why?

keys up and down and other work fine

pixel

ps
debuger shows propper values in keyData
Nov 16 '05 #1
4 15222
pixel <pi****@poczta. onet.pl> wrote:
i just discovered something strange

when i use in ProcessCmdKey
this construction

switch(keyData)
{
case Keys.Left:
{
...
}break;
case Keys.Right:
{
...
}break;
}

everuthing is fine
but in this construction

if((keyData & Keys.Left) == Keys.Left)
{
...
}
else if ((keyData & Keys.Right) == Keys.Right)
{
...
}

program goes into "left" no matter if i press left or right arrow
why?

keys up and down and other work fine


Because the Keys enumeration isn't meant to be used like that. The
value of Keys.Left is 37, and the value of Keys.Right is 39. Look at
the bit patterns:

Keys.Left = 00100101
Keys.Right= 00100111

So Keys.Right & Keys.Left is in fact Keys.Left. Everything is happening
as it should be - it's just that your test doesn't do what you think it
should.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
thanx man

so if i wanna check shift with left/right
i have to neg shift bits right?

pixel
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
pixel <pi****@poczta. onet.pl> wrote:
i just discovered something strange

when i use in ProcessCmdKey
this construction

switch(keyData)
{
case Keys.Left:
{
...
}break;
case Keys.Right:
{
...
}break;
}

everuthing is fine
but in this construction

if((keyData & Keys.Left) == Keys.Left)
{
...
}
else if ((keyData & Keys.Right) == Keys.Right)
{
...
}

program goes into "left" no matter if i press left or right arrow
why?

keys up and down and other work fine


Because the Keys enumeration isn't meant to be used like that. The
value of Keys.Left is 37, and the value of Keys.Right is 39. Look at
the bit patterns:

Keys.Left = 00100101
Keys.Right= 00100111

So Keys.Right & Keys.Left is in fact Keys.Left. Everything is happening
as it should be - it's just that your test doesn't do what you think it
should.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
pixel <pi****@poczta. onet.pl> wrote:
so if i wanna check shift with left/right
i have to neg shift bits right?


You should use:

if ((key & Keys.KeyCode)== Keys.Left)
{
// It's a "left" key press
}

and

if ((key & Keys.Modifier & Keys.Shift)==Ke ys.Shift)
{
// The shift key is down
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
thx very much

pixel
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
pixel <pi****@poczta. onet.pl> wrote:
so if i wanna check shift with left/right
i have to neg shift bits right?


You should use:

if ((key & Keys.KeyCode)== Keys.Left)
{
// It's a "left" key press
}

and

if ((key & Keys.Modifier & Keys.Shift)==Ke ys.Shift)
{
// The shift key is down
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3320
by: Steve Loft | last post by:
I'm trying to use a switch construct to compare the result of a form with a list of known values and display a different page according to the submitted string. The problem is that some of the answers I need to check for are strings of the character zero of varying length: E.g. <?php switch (trim(strtolower($_POST))):
35
8367
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except for '5'. So in his switch statement, he omits a case for '5':
12
3244
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
18
3077
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
1
1449
by: Craig S | last post by:
I have something like this: if (strVariable.BeginsWith("SOMETHING") //whatever if (strVariable.BeginsWith("ELSE") //whatever if (strVariable.BeginsWith("SomethingEvenLonger") //whatever
10
12331
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a program to demonstrate how a switch without breaks behaves vs. how I expected it to behave. The code includes: (1) a switch statement with breaks (2) the if/else statements that have the same results as (1) (3) a switch statement without breaks...
5
4524
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the blank lines on. I do a switch on the part id. I have 53 in all out of the parts list. What fills the blank is...
21
7742
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
6
1804
by: asadikhan | last post by:
Hello, I have a bit of a design issue around this application I am developing, and I just want to run it through some of the brains out here. So I have a table called ErrorCheck which contains fields ErrorID (PK) and ErrorName. There is another table called Client which contains fields ClientID (PK) and ClientName. Then there is a relationship table called ClientErrorCheck which contains foreign keys ErrorID and
0
9842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10566
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10623
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4040
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.