Skip to content

Commit

Permalink
Improved the Modifier KeyPress by counting bits.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clancey committed Feb 25, 2011
1 parent a9a13d8 commit 63f2545
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
10 changes: 3 additions & 7 deletions MonoMac.Windows.Forms/CocoaHelpers/TextBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,15 @@ public override void KeyDown (NSEvent theEvent)
public override void FlagsChanged (NSEvent theEvent)
{
var theKey = (NSEventModifierMask)Enum.ToObject(typeof(NSEventModifierMask),(uint)theEvent.ModifierFlags & 0xFFFF0000);
int count = 0;
foreach(NSEventModifierMask key in Enum.GetValues(typeof(NSEventModifierMask)))
{
count += theKey.HasFlag(key) ? 1 : 0;
}

int count = Util.NumberOfSetBits((int)theKey) ;
//Console.WriteLine(count);
if(theKey == 0 || lastKeyCount > count){
Host.Host.onKeyUp(new KeyEventArgs(theEvent));
Host.onKeyUp(new KeyEventArgs(theEvent));
//Console.WriteLine("keyUp");
}
else {
Host.Host.onKeyDown(new KeyEventArgs(theEvent));
Host.onKeyDown(new KeyEventArgs(theEvent));
//Console.WriteLine("keyDown");
}
lastKeyCount = count;
Expand Down
8 changes: 2 additions & 6 deletions MonoMac.Windows.Forms/System.Windows.Forms/Form.cocoa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ public override void KeyUp (NSEvent theEvent)
public override void FlagsChanged (NSEvent theEvent)
{
var theKey = (NSEventModifierMask)Enum.ToObject(typeof(NSEventModifierMask),(uint)theEvent.ModifierFlags & 0xFFFF0000);
int count = 0;
foreach(NSEventModifierMask key in Enum.GetValues(typeof(NSEventModifierMask)))
{
count += theKey.HasFlag(key) ? 1 : 0;
}

int count = Util.NumberOfSetBits((int)theKey) ;
//Console.WriteLine(count);
if(theKey == 0 || lastKeyCount > count){
Host.onKeyUp(new KeyEventArgs(theEvent));
Host.onKeyUp(new KeyEventArgs(theEvent));
//Console.WriteLine("keyUp");
}
else {
Expand Down
7 changes: 7 additions & 0 deletions MonoMac.Windows.Forms/Utils/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public static object GetPropertyValue(object inObject, string propertyName)
return prop.GetValue(inObject,null).ToString();
return null;
}

public static int NumberOfSetBits(int i)
{
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return ((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}

public static SizeF MeasureString(string inString, Font font)//, SizeF size)
{
Expand Down

0 comments on commit 63f2545

Please sign in to comment.