Winform like keyboard-events with JS event interception with fine-grained preventDefault control directly baked into C# Blazor Server/Webassembly. #62636
Unanswered
hintsofttech
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
if we could do this enterprise level of keyboard navigations, key-level operations, key-prevention for typing everything would be very appreciable or if not possible in anyway it should provide keyboard helper package to accomplish the task.
like suggested by ChatGPT below or better so that developer need not to worry what's going inside js. developer should feel all c# native :-
// wwwroot/keyboardHelper.js
export function initKeyboardHandler(dotNetHelper, options) {
const keysToPrevent = options.keysToPrevent || [];
const keyMappings = options.keyMappings || {};
function getComboKey(e) {
let combo = '';
if (e.ctrlKey) combo += 'Ctrl+';
if (e.shiftKey) combo += 'Shift+';
if (e.altKey) combo += 'Alt+';
combo += e.key.toLowerCase();
return combo;
}
function onKeyDown(e) {
const combo = getComboKey(e);
}
window.addEventListener('keydown', onKeyDown);
return {
dispose: () => window.removeEventListener('keydown', onKeyDown)
};
}
KeyboardHelper service
csharp
Copy
Edit
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.JSInterop;
namespace BlazorKeyboardHelper
{
public class KeyboardHelper : IAsyncDisposable
{
private readonly IJSRuntime _js;
private IJSObjectReference _module;
private DotNetObjectReference _objRef;
}
public enum KeyboardAction
{
// Letters
A, B, C, D, E, F, G, H, I, J, K, L, M,
N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
builder.Services.AddScoped();
protected override async Task OnInitializedAsync()
{
KeyboardHelper.OnKeyPressed += HandleKeyPressed;
}
Beta Was this translation helpful? Give feedback.
All reactions