I'm making an electron desktop app. I want to disable windows key and function keys while the app is on
I tried using the following code ... it registers the event but the windows menu opens anyways
$(document).keydown(function(e){
if (e.keyCode == 37) {
alert( "windows key pressed" );
return false;
}
});
Any help?
You can try this, but unforunately it will become a global shortcut, meaning when the window doesn't have focus it will still be registered. Try putting a console.log() to see when it fires. win is your electron window variable
const {app, globalShortcut} = require('electron');
win = new BrowserWindow();
globalShortcut.register('Super', () => {
if (win.isFocused()) {
// do something
}
});
You can check the docs here: docs
Or try to use this module here: electron-localshortcut
electronLocalshortcut.register(win, 'Super', () => {
console.log('Windows Button pressed');
return false;
});
Related
I am trying to disable all keyboard shortcuts in my electron.js app.
I tried the following ways (spoiler alert: they didn't work):
globalShortcut.unregisterAll()
and
globalShortcut.register('Alt+CommandOrControl+A', () => {
console.log('not allowed')
})
globalShortcut.register('Alt+CommandOrControl+B', () => {
console.log('not allowed')
})
globalShortcut.register('Alt+CommandOrControl+C', () => {
console.log('not allowed')
})
globalShortcut.register('Alt+CommandOrControl+D', () => {
console.log('not allowed') // and so on
})
(i did this ^ for all the keys (from A to Z, 1 to 9, etc).
By the way, all of the code I tried I put into the app.whenReady() function.
Well, none of this worked. I saw a lot of articles with other more abstract ways, but they didn't work either.
I actually tried searching for an npm package too, but I didn't find any that would solve my problem.
I just need to completely disable all keyboard shortcuts from my electron app. Is there any other way (that actually works)?
Here is simple solution ::
app.on('browser-window-focus', function () {
globalShortcut.register("CommandOrControl+R", () => {
console.log("CommandOrControl+R is pressed: Shortcut Disabled");
});
globalShortcut.register("F5", () => {
console.log("F5 is pressed: Shortcut Disabled");
});
});
app.on('browser-window-blur', function () {
globalShortcut.unregister('CommandOrControl+R');
globalShortcut.unregister('F5');
});
You can disable all the shortcuts by registering while the window is focused and unregister when blur.
See this question right here Disable reload via keyboard shortcut electron app
EDIT : disable all shortcuts globalShortcut.registerAll(accelerators, callback)
accelerators String[] - an array of Accelerators.
callback Function
Registers a global shortcut of all accelerator items in accelerators. The callback is called when any of the registered shortcuts are pressed by the user.
globalShortcut.unregisterAll()
Unregisters all of the global shortcuts.
I need to launch the electron app or focus it ( if already launched ) from a browser link. I have searched and tried many solutions but not getting it to work, so if someone has any experience with it, can you please help?
Here is the code:
// Single instance app ==========
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
}
// Register private URI scheme for the current user when running for the first time
app.setAsDefaultProtocolClient('x-protocol');
When I try to launch using this code, I get the goTheLock value as false, but the second-instance event is not getting fired, not sure why.
Version Details:
platform: Windows 10
electron: 8.5.3
electron-builder: 21.2.0
Update:
I added a delay of 5 seconds before quitting the app inside !gotTheLock, and in that case, I'm getting the event.
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
delay(5000); // 5 seconds delay
app.quit();
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
}
I don't understand. If you want to launch the app from a browser link then why you're implementing a second-instance? second-instance will fire if you open an application for a second time.
like this,
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
if (win) {
app.quit();
}
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (win) {
win.show();
win.focus();
}
})
}
I would like to disable shortcut "CTRL+A" on specific window,
Here is my current code based on Electron docs
electronLocalshortcut.register(app..main, 'Ctrl+A', () => {
console.log('prevent ctrl+a');
});
I'm able to catch "CTRL+A" event but I'm not able to prevent select all effect it still select all items on the page, app bar etc
In your renderer (window) process, add a keydown listener, it will allow you to prevent any shortcuts you want:
document.addEventListener('keydown', keyDownHandler)
function keyDownHandler (event) {
if (event.ctrlKey && event.code === 'KeyA') {
event.preventDefault()
}
}
Need to navigate to other view as soon as Scan completes
Using Zxing with ZXingScannerView
Using this code
scannerView.StartScanning(async (result) =>
{
if (!ContinuousScanning)
{
Console.WriteLine("Stopping scan...");
Console.WriteLine("Result: " + result.Text);
scannerView.StopScanning();
if (result != null)
{
await GetScannedDetails(result.Text);
// here i need to navigate to other screen
}
}
var evt = this.OnScannedResult;
if (evt != null) evt(result);
}, this.ScanningOptions);
When i tried navigating I got this error
Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
The issue you are having is, you try to run UI related code inside async task. Do the navigation inside main thread
BeginInvokeOnMainThread(
() =>
{
scannerView.StopScanning();
// Navigate code goes here
});
I'm developing my first FirefoxOS app and I need it to backup some data when the user closes or pauses the app. How can I detect this?
I've tried "window.onunload" but it doesn't seem to work.
Thank you.
You should be able to use page visibility API for background switch.
document.addEventListener("visibilitychange", function () {
if (document.hidden) {
console.log("App is hidden");
} else {
console.log("App has focus");
} });
Event launch visible only if app lost focus.
document.addEventListener('visibilitychange', function() {
if (document['visibilityState'] == "hidden") {
//code if is user out app, no close app
} else if(document['visibilityState'] == 'visible') {
//code if user return on app
}
}, false);