Wrapping keyboardevent with InputEvent

This commit is contained in:
斟酌 鵬兄 2016-03-22 19:31:19 +08:00
parent 75662e6d04
commit 5f5e0604fa
5 changed files with 69 additions and 38 deletions

View File

@ -5,8 +5,6 @@
var debug = __import( "System.Debug" ); var debug = __import( "System.Debug" );
var Mesg = __import( "Components.Vim.Message" ); var Mesg = __import( "Components.Vim.Message" );
/** @type {Components.Vim.Controls} */
var Controls = __import( "Components.Vim.Controls" );
/** @type {Components.Vim.Cursor.IAction} */ /** @type {Components.Vim.Cursor.IAction} */
var YANK = ns[ NS_INVOKE ]( "YANK" ); var YANK = ns[ NS_INVOKE ]( "YANK" );
@ -41,15 +39,15 @@
{ {
e.preventDefault(); e.preventDefault();
if( Controls.ModKeys( e ) ) return; if( e.ModKeys ) return;
var Action = null; var Action = null;
switch( true ) switch( true )
{ {
case Controls.KMap( e, "y" ): case e.kMap( "y" ):
Action = new YANK( this.__cursor ); Action = new YANK( this.__cursor );
break; break;
case Controls.KMap( e, "d" ): case e.kMap( "d" ):
Action = new DELETE( this.__cursor ); Action = new DELETE( this.__cursor );
break; break;
} }

View File

@ -7,6 +7,7 @@
var SHIFT = 1 << 9; var SHIFT = 1 << 9;
var CTRL = 1 << 10; var CTRL = 1 << 10;
var ALT = 1 << 11;
var KEY_SHIFT = 16; var KEY_SHIFT = 16;
var KEY_CTRL = 17; var KEY_CTRL = 17;
@ -139,7 +140,7 @@
if( compReg.i == keys.length ) if( compReg.i == keys.length )
{ {
compReg.handler(); compReg.handler();
compReg = null; this.__compReg = null;
this.__cMovement = false; this.__cMovement = false;
} }
@ -153,13 +154,13 @@
return false; return false;
}; };
Controls.prototype.__actionCommand = function( e, kCode ) Controls.prototype.__actionCommand = function( e )
{ {
var ActionHandled = true; var ActionHandled = true;
var ccur = this.__ccur; var ccur = this.__ccur;
// Action Command // Action Command
switch( kCode ) switch( e.keyCode )
{ {
case SHIFT + A: // Append at the line end case SHIFT + A: // Append at the line end
ccur.lineEnd(); ccur.lineEnd();
@ -226,12 +227,13 @@
} }
}; };
Controls.prototype.__cursorCommand = function( e, kCode ) Controls.prototype.__cursorCommand = function( e )
{ {
var kCode = e.keyCode;
if( this.__cMovement && this.__comp ) if( this.__cMovement && this.__comp )
{ {
var k = e.keyCode; if( !e.ModKeys )
if(!( k == KEY_SHIFT || k == KEY_CTRL || k == KEY_ALT ))
{ {
this.__comp( kCode ); this.__comp( kCode );
return true; return true;
@ -285,19 +287,20 @@
return cursorHandled; return cursorHandled;
}; };
/**
* sender @param {Components.Vim.VimArea}
* e @param {Components.Vim.Controls.InputEvent}
* */
Controls.prototype.handler = function( sender, e ) Controls.prototype.handler = function( sender, e )
{ {
// Neve capture these keys // Neve capture these keys
if( e.altKey if( e.ModKeys
// F2 - F12 // F2 - F12
|| ( F1 < e.keyCode && e.keyCode < 124 ) || ( F1 < e.keyCode && e.keyCode < 124 )
) return; ) return;
// Esc OR Ctrl + c
var Escape = e.keyCode == ESC || ( e.ctrlKey && e.keyCode == C );
// Clear composite command // Clear composite command
if( Escape && this.__compReg ) if( e.Escape && this.__compReg )
{ {
this.__compReg = null; this.__compReg = null;
this.__cMovement = false; this.__cMovement = false;
@ -308,14 +311,12 @@
var cfeeder = this.__cfeeder; var cfeeder = this.__cfeeder;
var ccur = this.__ccur; var ccur = this.__ccur;
var kCode = e.keyCode var kCode = e.keyCode;
+ ( e.shiftKey || e.getModifierState( "CapsLock" ) ? SHIFT : 0 )
+ ( e.ctrlKey ? CTRL : 0 );
// Action commands are handled by the actions themselves // Action commands are handled by the actions themselves
if( ccur.action ) if( ccur.action )
{ {
if( Escape ) if( e.Escape )
{ {
e.preventDefault(); e.preventDefault();
ccur.closeAction(); ccur.closeAction();
@ -333,26 +334,49 @@
return; return;
} }
e.preventDefault(); if( this.__cursorCommand( e ) )
{
e.preventDefault();
return;
}
if( this.__cursorCommand( e, kCode ) ) return; if( this.__actionCommand( e ) )
if( this.__actionCommand( e, kCode ) ) return; {
e.preventDefault();
return;
}
}; };
__static_method( Controls, "ModKeys", function( e ) var InputEvent = function( e )
{ {
var c = e.keyCode; this.__e = e;
return c == KEY_SHIFT || c == KEY_CTRL || c == KEY_ALT;
} );
__static_method( Controls, "KMap", function( e, map ) var c = this.__e.keyCode;
{
var kCode = e.keyCode this.__escape = c == ESC || ( e.ctrlKey && c == C );
this.__kCode = c
+ ( e.shiftKey || e.getModifierState( "CapsLock" ) ? SHIFT : 0 ) + ( e.shiftKey || e.getModifierState( "CapsLock" ) ? SHIFT : 0 )
+ ( e.ctrlKey ? CTRL : 0 ); + ( e.ctrlKey ? CTRL : 0 );
return kCode == Map( map ); this.__modKeys = c == KEY_SHIFT || c == KEY_CTRL || c == KEY_ALT;
} ); this.__key = e.key;
};
__readOnly( InputEvent.prototype, "key", function() { return this.__key; } );
__readOnly( InputEvent.prototype, "keyCode", function() { return this.__kCode; } );
__readOnly( InputEvent.prototype, "ModKeys", function() { return this.__modKeys; } );
__readOnly( InputEvent.prototype, "Escape", function() { return this.__escape; } );
InputEvent.prototype.kMap = function( map )
{
return this.__kCode == Map( map );
};
InputEvent.prototype.preventDefault = function()
{
if( this.__e ) this.__e.preventDefault();
};
ns[ NS_EXPORT ]( EX_CLASS, "Controls", Controls ); ns[ NS_EXPORT ]( EX_CLASS, "Controls", Controls );
ns[ NS_EXPORT ]( EX_CLASS, "InputEvent", InputEvent );
})(); })();

View File

@ -19,6 +19,7 @@
var StatusBar = ns[ NS_INVOKE ]( "StatusBar" ); var StatusBar = ns[ NS_INVOKE ]( "StatusBar" );
var VimControls = ns[ NS_INVOKE ]( "Controls" ); var VimControls = ns[ NS_INVOKE ]( "Controls" );
var InputEvent = ns[ NS_INVOKE ]( "InputEvent" );
var mesg = ns[ NS_INVOKE ]( "Message" ); var mesg = ns[ NS_INVOKE ]( "Message" );
var Insts = []; var Insts = [];
@ -31,7 +32,7 @@
if ( e.keyCode ) code = e.keyCode; if ( e.keyCode ) code = e.keyCode;
else if ( e.which ) code = e.which; else if ( e.which ) code = e.which;
handler( sender, e ); handler( sender, new InputEvent( e ) );
}; };
}; };

View File

@ -0,0 +1,13 @@
/** @constructor */
Components.Vim.Controls.InputEvent = function(){};
/** @type String */
Components.Vim.Controls.InputEvent.key;
/** @type Boolean */
Components.Vim.Controls.InputEvent.ModKeys;
/** @type Boolean */
Components.Vim.Controls.InputEvent.Escape;
/** @type Number */
Components.Vim.Controls.InputEvent.keyCode;
/** @type Function */
Components.Vim.Controls.InputEvent.kMap;

View File

@ -1,7 +1,2 @@
/** @constructor */ /** @constructor */
Components.Vim.Controls = function(){}; Components.Vim.Controls = function(){};
/** @type Function */
Components.Vim.Controls.ModKeys;
/** @type Function */
Components.Vim.Controls.KMap;