2015-01-07 12 views
5

ブラウザウィンドウ内でbackspaceキーのback()機能を無効にする方法を調べるために、interwebzとAtom-shellのドキュメントを精査しています。アトムシェルでバックスペースを無効にする

私は、ブラウザのウィンドウレベルではなく、アプリケーションレベルの多くのネイティブのものを使用して、javascriptのonkeydownリスナーに頼らざるを得ないことを望みます。

答えて

2

onkeydownリスナーなしでこれを行うために私が考えた唯一の方法は、グローバルショートカットとElectron APIのipcイベントです。グローバルなショートカットで任意のキーを無効

まず免責事項...

は本当にあなたのコンピュータ上でグローバルに無効ん! 世界的なショートカットを使用する際は、十分注意してください! ショートカットの登録を忘れる、または正しく処理しない場合、バックスペースなしで間違いを修正するのは難しいでしょう!これは私のために働いていたものです言っ

...

const { app, ipcMain, 
    globalShortcut, 
    BrowserWindow, 
} = require('electron'); 

app.on('ready',() => { 

    // Create the browser window 
    let mainWindow = new BrowserWindow({width: 800, height: 600}); 

    // and load the index.html of the app 
    mainWindow.loadUrl('file://' + __dirname + '/index.html'); 

    // Register a 'Backspace' shortcut listener when focused on window 
    mainWindow.on('focus',() => { 

     if (mainWindow.isFocused()) { 
      globalShortcut.register('Backspace',() => { 

       // Provide feedback or logging here 
       // If you leave this section blank, you will get no 
       // response when you try the shortcut (i.e. Backspace). 

       console.log('Backspace was pressed!'); //comment-out or delete when ready. 
      }); 
     }); 
    }); 

    // ** THE IMPORTANT PART ** 
    // Unregister a 'Backspace' shortcut listener when leaving window. 
    mainWindow.on('blur',() => { 

     globalShortcut.unregister('Backspace'); 
     console.log('Backspace is unregistered!'); //comment-out or delete when ready. 
    }); 
}); 

また、あなたがこのようなIPC「切り替え」イベントハンドラ内のショートカットを追加することができます...

// In the main process 
ipcMain.on('disableKey-toggle', (event, keyToDisable) => { 
    if (!globalShortcut.isRegistered(keyToDisable){ 

     globalShortcut.register(keyToDisable,() => { 
      console.log(keyToDisable+' is registered!'); //comment-out or delete when ready. 

     }); 
    } else { 

     globalShortcut.unregister(keyToDisable); 
     console.log(keyToDisable+' is unregistered!'); //comment-out or delete when ready. 
    } 
}); 

// In the render process send the accelerator of the keyToDisable. 
// Here we use the 'Backspace' accelerator. 
const { ipcRenderer } = require('electron'); 
ipcRenderer.send('disableKey-toggle', 'Backspace'); 
+0

なぜだろうアプリケーション全体のバックスペースキーをブロックしますか?あなたはフロントエンド/レンダラーで、通常の "javascript"でそれをブロックすることができます。[ここでstackoverflow](http://stackoverflow.com/a/2768256/1435377)(またはその答えの下の同様の答え)で説明します。 :) 私はトピックを知っています。スターターはnodejsの方法を尋ねました...まだ理由を知りません - 人々はちょうどこの方法を念頭に置いておくべきです...(: –

+1

私はこれが通常最も簡単な方法ですが、この質問は、Electronと* not * Javascriptを使用してそれを行う方法を尋ねました。より適用可能なユースケースがあると思います。ウィンドウを開くと、別のアプリケーションまたはシステムのダイアログでキーを傍受し続ける(ブロックしない) -押す。 – Josh

関連する問題