-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathInputListener.cs
More file actions
106 lines (91 loc) · 3.52 KB
/
Copy pathInputListener.cs
File metadata and controls
106 lines (91 loc) · 3.52 KB
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
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
//using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
namespace PeasyMotion
{
class InputListener : IOleCommandTarget
{
public readonly IVsTextView vsTextView;
public ITextView textView;
IOleCommandTarget nextCommandHandler;
public event KeyPressEventHandler KeyPressed;
/// <summary>
/// Add this filter to the chain of Command Filters
/// </summary>
internal InputListener(IVsTextView vsTextView_, ITextView textView_)
{
vsTextView = vsTextView_;
textView = textView_;
}
public void AddFilter()
{
vsTextView.AddCommandFilter(this, out nextCommandHandler);
}
public void RemoveFilter()
{
vsTextView.RemoveCommandFilter(this);
nextCommandHandler = null;
}
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
ThreadHelper.ThrowIfNotOnUIThread();
return nextCommandHandler.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
}
/// <summary>
/// Get user input.
/// IOleCommandTarget.Exec() function
/// </summary>
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
Debug.WriteLine($"InputListener.Exec( {pguidCmdGroup} {nCmdID} {nCmdexecopt} {pvaIn} {pvaOut} )");
ThreadHelper.ThrowIfNotOnUIThread();
int hr = VSConstants.S_OK;
if ((new[] {
4, // tab
7, // left arrow
11, // up arrow
9, // right arrow
13, // down arrow
103, // escape
32, // space
}).Contains((int)nCmdID))
{
// send '\0' so we can abort
KeyPressed?.Invoke(this,new KeyPressEventArgs('\0'));
return hr;
}
char typedChar;
if (TryGetTypedChar(pguidCmdGroup, nCmdID, pvaIn, out typedChar))
{
KeyPressed?.Invoke(this, new KeyPressEventArgs(typedChar));
return hr;
}
ThreadHelper.ThrowIfNotOnUIThread();
hr = nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
return hr;
}
/// <summary>
/// Try to get the keypress value. Returns 0 if attempt fails
/// </summary>
/// <param name="typedChar">Outputs the value of the typed char</param>
/// <returns>Boolean reporting success or failure of operation</returns>
bool TryGetTypedChar(Guid cmdGroup, uint nCmdID, IntPtr pvaIn, out char typedChar)
{
typedChar = char.MinValue;
//Debug.WriteLine("InputListener.cs | TryGetTypedChar | nCmdId " + nCmdID);
//Debug.WriteLine("InputListener.cs | TryGetTypedChar | pvaIn " + pvaIn);
if (cmdGroup != VSConstants.VSStd2K || nCmdID != (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
return false;
typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
return true;
}
}
}