473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

A little design question

I want to write a class to manage keyboard SHIFT, ALT and CTRL modifier keys
in an event management system, distinguishing between left and right keys of
each one. This seems trivial, but I've found a design problem (from a formal
perspective) I dealed with before, in other contexts, and for which I can't
find an optimum solution. Specifically, I don't know which is the optimum
way to provide access to each modifier key state.

My first alternative is to include explicit access methods for each modifier
key:

enum TKeyState { KEY_STATE_RELEASED, KEY_STATE_PUSHED };

class CKeyboardModifierKeys
{
// ...

TKeyState GetLeftSHIFTState () const;
TKeyState GetRightSHIFTState () const;
TKeyState GetSHIFTState () const;
TKeyState GetLeftALTState () const;
TKeyState GetRightALTState () const;
TKeyState GetALTState () const;
TKeyState GetLeftCTRLState () const;
TKeyState GetRightCTRLState () const;
TKeyState GetCTRLState () const;

void SetLeftSHIFTState (const TKeyState value);
void SetRightSHIFTState (const TKeyState value);
void SetLeftALTState (const TKeyState value);
void SetRightALTState (const TKeyState value);
void SetLeftCTRLState (const TKeyState value);
void SetRightCTRLState (const TKeyState value);

// ...
};

My second alternative is to refer the different keys through members of an
enumeration:

class CKeyboardModifierKeys
{
// ...

enum TModifierKey { SHIFT, ALT, CTRL };
enum TWhichKey { LEFT, RIGHT, ANY };

TKeyState GetKeyState (const TModifierKey key, const TWhichKey which)
const;
void GetKeyState (const TModifierKey key, const TWhichKey which, const
TKeyState value);

// ...
};

I don't know additional ways, but I'm sure that other alternatives can be
considered. Can someone give me advice? Thank yo in advance :-)
Jul 22 '05 #1
2 1296
Rubén Campos wrote:
I want to write a class to manage keyboard SHIFT, ALT and CTRL modifier keys
in an event management system, distinguishing between left and right keys of
each one. This seems trivial, but I've found a design problem (from a formal
perspective) I dealed with before, in other contexts, and for which I can't
find an optimum solution. Specifically, I don't know which is the optimum
way to provide access to each modifier key state.

My first alternative is to include explicit access methods for each modifier
key:

enum TKeyState { KEY_STATE_RELEASED, KEY_STATE_PUSHED };

class CKeyboardModifierKeys
{
// ...

TKeyState GetLeftSHIFTState () const;
TKeyState GetRightSHIFTState () const;
TKeyState GetSHIFTState () const;
TKeyState GetLeftALTState () const;
TKeyState GetRightALTState () const;
TKeyState GetALTState () const;
TKeyState GetLeftCTRLState () const;
TKeyState GetRightCTRLState () const;
TKeyState GetCTRLState () const;

void SetLeftSHIFTState (const TKeyState value);
void SetRightSHIFTState (const TKeyState value);
void SetLeftALTState (const TKeyState value);
void SetRightALTState (const TKeyState value);
void SetLeftCTRLState (const TKeyState value);
void SetRightCTRLState (const TKeyState value);

// ...
};

My second alternative is to refer the different keys through members of an
enumeration:

class CKeyboardModifierKeys
{
// ...

enum TModifierKey { SHIFT, ALT, CTRL };
enum TWhichKey { LEFT, RIGHT, ANY };

TKeyState GetKeyState (const TModifierKey key, const TWhichKey which)
const;
void GetKeyState (const TModifierKey key, const TWhichKey which, const
TKeyState value);

// ...
};

I don't know additional ways, but I'm sure that other alternatives can be
considered. Can someone give me advice? Thank yo in advance :-)


Perhaps you may want to have each key as an instance of a
generic modifier key class. This class would retain the
state of the key.

You could have each instance as a singleton, since there
is only one of each (since you differentiate left shift
keys from right ones).

class ModifierKey
{
enum Key_State {Not_Pressed, Pressed, Released};

// other methods / members
};

class Right_Shift_Key
: public Singleton<Right_Shift_Key>
{
//...
};
Some keyboard processing applications have different tables
based on the combinations of the modifier keys.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #2
"Rubén Campos" <Ru**********@robotica.uv.es> wrote in message news:<ck**********@peque.uv.es>...
I want to write a class to manage keyboard SHIFT, ALT and CTRL modifier keys
in an event management system, distinguishing between left and right keys of
each one. This seems trivial, but I've found a design problem (from a formal
perspective) I dealed with before, in other contexts, and for which I can't
find an optimum solution. Specifically, I don't know which is the optimum
way to provide access to each modifier key state.

My first alternative is to include explicit access methods for each modifier
key: [snip] My second alternative is to refer the different keys through members of an
enumeration:

[snip]

This is an evergreen problem. It does not have a neat packaged pat
answer. It depends on context.

One way to approach it is, build a metaphor. Stick with it, this is
not as stupid as it sounds.

Think of your class as an exporter of a service. Figure out what that
service is going to be in the context of the problem you are solving,
and the generic background of likely modifications to the problem.
Keyboards: Will you likely have to handle different kinds of keyboards?
(Extra keys, programmable keys, key combinations, key sequences, etc.)
What is the service exactly the class is providing? Is it just holding
on to some data values? Maybe a plain old data (POD) structure is better,
with public data members. Or is the service some kind of state machine
that does different things depending on the history of keys that have
been pressed. So, those programmable keys, or key combos or something.

The context and the service will tell you what the class is supposed to
do in a non-code-specific way. Keep track of bold on/off, definitions
of programmable keys, etc. Or just hold data. Or keep track of key repeat
speed, accents, pound or hash sign, etc. etc. Other languages? Other
symbol sets? Whatever.

When you know *what* it should do, then think about how.

Maybe you want something very different from a POD that takes the
current keystroke and returns some translation of it. Or maybe you
want things like buffering of keystrokes. Or maybe you want to be
able to querry the thing for history of the last "N" keystrokes.

Once you have the right metaphor (the class behaves as a secretary
for the keyboard, controlling what clients see when the keyboard
does this quirky thing, or, the class behaves as a big old bucket
holding on to stuff associated with the keyboard, and clients can
just reach in and get what they want) then you will see much more
easily how to implement it.
Socks
Jul 22 '05 #3

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

Similar topics

2
by: Slash | last post by:
I often do a lot of text-mode programming in Linux and wanted to use colors in text mode, and found the ncurses library needlessly complex for small applications. So I wrote my own little header to...
5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
10
by: Tom Dacon | last post by:
I'm curious to see if anyone has an opinion on this little design question - I'm doing a computational astronomy library in C#, purely for my own use, and one of the things that happens regularly...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
7
by: find.dhiraj | last post by:
Hi, I need little bit of help regarding the question below , any of your additional insight beside mine to the problem will be a great help for me Consider the following, rather limited,...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.