/    Sign up×
Community /Pin to ProfileBookmark

Manually intiate keyboard events

hi,
i want minimize browser on one button click.
for browser minimize kty is alt+space+n and i want to intiate this keys using init keys is it possible?
i ahev tried to do this like below

[code]
<script type=”text/javascript”>
function minimizew(event){
try{

if(!event){
event=window.event;
}
var e = document.createEvent(“KeyboardEvent”);
var text=document.getElementById(“minimize”);
document.body.focus();
// document.body.addEventListener(‘keydown’, function(e){alert(e.keyCode);}, true);
e.initKeyEvent(“keypress”, // in DOMString typeArg,

true, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
null, // in nsIDOMAbstractView viewArg, Specifies UIEvent.view. This value may be null.
false, // in boolean ctrlKeyArg,
false, // in boolean altKeyArg,
false, // in boolean shiftKeyArg,
false, // in boolean metaKeyArg,
18, null); // key code.
document.body.dispatchEvent(e);
event.returnValue = false;
e.initKeyEvent(“keypress”, // in DOMString typeArg,

true, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
null, // in nsIDOMAbstractView viewArg, Specifies UIEvent.view. This value may be null.
false, // in boolean ctrlKeyArg,
false, // in boolean altKeyArg,
false, // in boolean shiftKeyArg,
false, // in boolean metaKeyArg,
32, null); // key code.
text.dispatchEvent(e);
event.returnValue = false;
e.initKeyEvent(“keypress”, // in DOMString typeArg,

true, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
null, // in nsIDOMAbstractView viewArg, Specifies UIEvent.view. This value may be null.
false, // in boolean ctrlKeyArg,
false, // in boolean altKeyArg,
false, // in boolean shiftKeyArg,
false, // in boolean metaKeyArg,
78, null); // key code.
text.dispatchEvent(e);
event.returnValue = false;

}
catch (err){
alert(err.description);
}
}
</script>

<input alt=”Close” type=”button” style=”text-decoration:none;font-weight: bold;font-size:11px; cursor:pointer;right: 25px; top: 3px;height:21px; position: absolute;width:20px” onclick=”minimizew(event);” id=”minimize” value=”-“/>
[/code]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @jsfgeeks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...