A little design question

R

Rubén Campos

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 :)
 
T

Thomas Matthews

Rubén Campos said:
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
 
P

puppet_sock

Rubén Campos said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top