[Gtk-sharp-list] Subclassing a textview cannot control ControlMask key event

Michael Hutchinson m.j.hutchinson at gmail.com
Mon Aug 25 13:39:27 EDT 2008


On Mon, Aug 25, 2008 at 5:26 AM, True Friend <true.friend2004 at gmail.com> wrote:
> Hi
> I am trying to subclass a textview to write custom characters. Here is the
> sample code which I am using in "protected override OnKeyPress(EventArgs
> evnt)" method in derived class.
> protected override bool OnKeyPressEvent (EventKey evnt)
>         {
>         Dictionary<char, char> dict = new Dictionary<char,char>();
>         dict['`'] = 'ٍ';
>         dict['1'] = '1';
>         dict['2'] = '2';
>         dict['3'] = '3';
>         dict['4'] = '4';
>     if(evnt.State != ModifierType.ControlMask)
>         {
>             char key = (char)evnt.Key;
>             this.Editable = false;
>             if(dict.ContainsKey(key))
>             {
>             this.Buffer.InsertInteractiveAtCursor(dict[key].ToString(),
> true);
>             }
>             else
>             {
>                 this.Editable = true;
>                 return base.OnKeyPressEvent (evnt);
>             }
> }
> I want to write only when Ctrl key is not pressed when it is pressed the
> event should be the default one, I've tried to do so but evnt.State !=
> ModifierType.ControlMask is not working. The output on console.Writeline is
> also given here. Normal key press is Mod2Mask but ctrl key press plus any
> other key press is ControlMask, Mod2Mask two in same line are written.
> Interesting is this it is accepting ControlMask (input) but allowing to
> write the key as well (the hacked evenet handler is processed even).
> Any ideas about it? I am missing something or something else involved here?

ModifierType is a "Flags" enum.

(see http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx
for some explanation)

Hence, use a bitwise "and" to check whether the ControlMask flag is
set on State; if the result is zero, the bit corresponding to
ControlMask is not set.

((evnt.State & ModifierType.ControlMask) == 0)

-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Gtk-sharp-list mailing list