1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
public class Flags
{
/* KeyValue => Modifiers
* 162 => ControlLeft
* 163 => ControlRight
* 164 => Alt
* 162|165 => AltGr
* This one is a specific one; indeed AltGr is shortcut for ControlLeft+Alt, therefore you have first 162 OnKeyDown, then 165 OnKeyDown.
* However, the remaining value is 165 for e.KeyValue (the event has the value of the last key who was down, even if you are holding another one at the same time
*
* */
#region attributs
private bool flagCtrlL;
private bool flagCtrlR;
private bool flagAlt;
private bool flagCtrlAlt;
private bool flagF;
private bool flagAltGr;
#endregion
public Flags()
{
this.flagAlt = false;
this.flagAltGr = false;
this.flagCtrlAlt = false;
this.flagCtrlL = false;
this.flagCtrlR = false;
this.flagF = false;
}
#region methode
public void MappingF(int keyValue)
{
if ((keyValue == 112) || (keyValue == 113) || (keyValue == 114) || (keyValue == 115) || (keyValue == 116) || (keyValue == 117) || ((keyValue == 118) || (keyValue == 119) || (keyValue == 120) || (keyValue == 121) || (keyValue == 122) || (keyValue == 123))) //F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12
flagF = true;
}
public void UnMappingF(int keyValue)
{
if ((keyValue == 112) || (keyValue == 113) || (keyValue == 114) || (keyValue == 115) || (keyValue == 116) || (keyValue == 117) || ((keyValue == 118) || (keyValue == 119) || (keyValue == 120) || (keyValue == 121) || (keyValue == 122) || (keyValue == 123))) //F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12
flagF = false;
}
public void MappingModifiers(int keyValue) //set the flags depending on KeyValue; every specific KeyValue used beloy corresponds to a modifier; please refer to summary constant for all value
{
switch(keyValue)
{
case 162: //keyValue for CtrlL
this.flagCtrlL = true;
break;
case 163: //keyValue for CtrlR
this.flagCtrlR = true;
break;
case 164: //keyValue for Alt
this.flagAlt = true;
break;
case 165: //keyValue for AltGr
this.flagAltGr = true;
break;
default:
break;
}
if ((this.flagAlt && this.flagCtrlL == true) || (this.flagAlt && this.flagCtrlR == true))
this.flagCtrlAlt = true;
}
public void UnMappingModifiers(int keyValue)
{
switch (keyValue)
{
case 162: //keyValue for CtrlL
this.flagCtrlL = false;
break;
case 163: //keyValue for CtrlR
this.flagCtrlR = false;
break;
case 164: //keyValue for Alt
this.flagAlt = false;
break;
case 165: //keyValue for AltGr
this.flagAltGr = false;
break;
default:
break;
}
if ((this.FlagCtrlL == false) || (this.FlagCtrlR == false) || (this.FlagAlt == false))
this.FlagCtrlAlt = false;
}
public bool WriteControlLeft(int keyValue)
{
if (this.flagCtrlL == true && (this.flagAlt == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false)) //only write down if the user is not making a combination
if (keyValue != 162) //if the user holds CtrlL, we don't want to write it
return true;
return false;
}
public bool WriteControlRight(int keyValue)
{
if (this.flagCtrlR == true && (this.flagAlt == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false)) //only write down if the user is not making a combination
if (keyValue != 163) //if the user holds CtrlR, we don't want to write it until keydown is another character
return true;
return false;
}
public bool WriteAlt(int keyValue)
{
if (this.flagAlt == true && (this.flagCtrlL == false) && (this.flagCtrlR == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false) ) //only write down if the user is not making a combination
if (keyValue != 164) //if the user hold Alt, we don't want to write it until keydown is another character
return true;
return false;
}
public bool WriteAltCtrl(int keyValue)
{
if (this.flagCtrlAlt == true && this.flagAltGr == false)
if (keyValue != 162 && keyValue != 163 && keyValue != 164 && keyValue != 165) //the user is holding Ctrl + Alt, we don't want to write it until keydown is another character
return true;
return false;
}
public bool WriteAltGr(int keyValue)
{
if (this.flagAltGr == true && this.flagCtrlAlt == false)
if (keyValue != 162 && keyValue != 163 && keyValue != 164 && keyValue != 165) //the user is holding AltGr, we don't want to write it until keydown is another character
return true;
return false;
}
public bool WriteF(int keyValue)
{
if ( this.flagF == true && (this.flagAlt == false) && (this.flagCtrlL == false) && (this.flagCtrlR == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false) )
if ((keyValue != 112) || (keyValue != 113) || (keyValue != 114) || (keyValue != 115) || (keyValue != 116) || (keyValue != 117) || ((keyValue != 118) || (keyValue != 119) || (keyValue != 120) || (keyValue != 121) || (keyValue != 122) || (keyValue != 123)) ) //F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12 are recorded just once, even if the user kept them pressed
return true;
return false;
}
#endregion
#region getters and setters
public bool FlagCtrlL
{
get{ return this.flagCtrlL; }
set { this.flagCtrlL = value; }
}
public bool FlagCtrlR
{
get { return this.flagCtrlR; }
set { this.flagCtrlR = value; }
}
public bool FlagAlt
{
get { return this.flagAlt; }
set { this.flagAlt = value; }
}
public bool FlagCtrlAlt
{
get { return this.flagCtrlAlt; }
set { this.flagCtrlAlt = value; }
}
public bool FlagAltGr
{
get { return this.flagAltGr; }
set { this.flagAltGr = value; }
}
public bool FlagF
{
get { return this.flagF; }
set { this.flagF = value; }
}
#endregion
}
} |
Partager