My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
/*
* Copyright (c) 2010-2012 Tesla Engine Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using SlimDX.Multimedia;
using Tesla.Input;
using RI = SlimDX.RawInput;
using SWF = System.Windows.Forms;

namespace Tesla.Windows.Input {
/// <summary>
/// Keyboard wrapper using Windows RawInput.
/// </summary>
public sealed class RawInputKeyboardWrapper : IKeyboardWrapper {
private static uint _maskValue = uint.MaxValue;

private uint _keyState0;
private uint _keyState1;
private uint _keyState2;
private uint _keyState3;
private uint _keyState4;
private uint _keyState5;
private uint _keyState6;
private uint _keyState7;

/// <summary>
/// Initializes a new instance of the <see cref="RawInputKeyboardWrapper"/> class.
/// </summary>
public RawInputKeyboardWrapper() {
RI.Device.RegisterDevice(UsagePage.Generic, UsageId.Keyboard, RI.DeviceFlags.None);
RI.Device.KeyboardInput += new EventHandler<RI.KeyboardInputEventArgs>(KeyboardInput);
}

/// <summary>
/// Gets the current state of the keyboard - e.g. which keys are pressed.
/// </summary>
/// <returns>
/// The current keyboard state.
/// </returns>
public KeyboardState GetKeyboardState() {
return new KeyboardState(_keyState0, _keyState1, _keyState2, _keyState3, _keyState4, _keyState5, _keyState6, _keyState7);
}

private void KeyboardInput(Object sender, RI.KeyboardInputEventArgs e) {
SWF.Keys key = e.Key;
RI.KeyState state = e.State;

if(state == RI.KeyState.Pressed) {
AddPressedKey((int) key);
} else if(state == RI.KeyState.Released) {
RemovePressedKey((int) key);
}
}

private void AddPressedKey(int key) {
//Get the bit place in the state for the key
uint place = ((uint) 1) << key;
//Get the state grouping for the key
int grouping = key >> 5;
//Add the key to the proper state
switch(grouping) {
case 0:
_keyState0 |= place & _maskValue;
return;
case 1:
_keyState1 |= place & _maskValue;
return;
case 2:
_keyState2 |= place & _maskValue;
return;
case 3:
_keyState3 |= place & _maskValue;
return;
case 4:
_keyState4 |= place & _maskValue;
return;
case 5:
_keyState5 |= place & _maskValue;
return;
case 6:
_keyState6 |= place & _maskValue;
return;
case 7:
_keyState7 |= place & _maskValue;
return;
}
}

private void RemovePressedKey(int key) {
//Get the bit place for the key
uint place = ((uint) 1) << (int) key;
//Get the state grouping for the key
int grouping = key >> 5;
//Remove the key from the proper state
switch(grouping) {
case 0:
_keyState0 &= ~(place & _maskValue);
return;
case 1:
_keyState1 &= ~(place & _maskValue);
return;
case 2:
_keyState2 &= ~(place & _maskValue);
return;
case 3:
_keyState3 &= ~(place & _maskValue);
return;
case 4:
_keyState4 &= ~(place & _maskValue);
return;
case 5:
_keyState5 &= ~(place & _maskValue);
return;
case 6:
_keyState6 &= ~(place & _maskValue);
return;
case 7:
_keyState7 &= ~(place & _maskValue);
return;
}
}
}
}

Change log

r208 by nicholas.woodfield on Jan 1, 2012   Diff
SVN re-organization
Go to: 
Project members, sign in to write a code review

Older revisions

r207 by nicholas.woodfield on Jan 1, 2012   Diff
SVN re-organization
All revisions of this file

File info

Size: 5448 bytes, 142 lines
Powered by Google Project Hosting