Jump to content

[1.7.2] KeyHandler to KeyEvents, how to replace keyUP() method?


AXELTOPOLINO

Recommended Posts

Hello Forge users,

I'm updating my mod from 1.6.4 to 1.7.2.

My main problem right now is the new way for handling key events.

I mean, they are working fine, but I don't know how to do stuff only when the key is up (after it has been pressed).

So the problem is that I don't know how to update the old KeyHandler method called keyUp().

 

I have found only this topic: http://www.minecraftforge.net/forum/index.php?topic=18138.0

but the solution suggested is really terrible (I don't want to check for keys in a onUpdate() function..)  :-\

 

Thanks for the help in advance,

Axel

Link to comment
Share on other sites

There's not much that can be done that I know of; for my own mod, I took the following approach:

 

1. Rethink my implementations to do the work when the key is pressed instead of released

 

2. For those that absolutely must use keyUp type functionality, attempt to encase it in a custom object that can be updated each tick when necessary, but also removed when no longer needed so as not to waste time every tick. Example: a skill that activates when key is released -> create a skill class, instantiate the object during key input event for that key and, now that the object exists, it updates each tick checking if the key is still pressed; as soon as it isn't, the skill does its thing and removes itself as needed.

 

3. Failing all the above, use a solution such as that in the topic you mentioned.

 

Sorry that may not be very helpful, but those are the only things I was able to come up with to attempt to replace the sadly extinct keyUp method. I hope we see it make a come-back some day.

Link to comment
Share on other sites

While it is good practice to avoid any extra processing every tick (due to the processing burden), the reality is that the code is already doing tons of stuff every tick and a simple event handler that is updating some boolean flag isn't going to break a modern computer.  I agree it would be better to have an built-in (i.e. performance optimized) keyUp type event but I wouldn't be so loathe to implement it yourself.  A pain yes but hardly a performance killer.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Thanks guys for your answers, I will probably follow coolAlias steps. It's just that I seriously don't like when the code is messy and having all the key events around the code is surely a terrible behaviour for any good coder :)

 

Hope Forge guys will re-add this useful feature anytime soon,

Axel

Link to comment
Share on other sites

EDIT: I still don't get how can I detect the key released in a onUpdate function. I tried this, but it will override also all MC keybinds:

 

@SubscribeEvent
public void tickClient(ClientTickEvent e)
{
	System.out.println("TESTING");
	while (Keyboard.next())
	{
		System.out.println("TESTING STUFF0");
		if (Keyboard.getEventKeyState())
		{
			System.out.println("TESTING STUFF1a");
			FMLCommonHandler.instance().fireKeyInput(); //KeyInputEvent (on key press)
		} else {
			System.out.println("TESTING STUFF1b");
			SWKeyHandler.keyUp(Keyboard.getEventKey()); //My KeyOutputEvent (on key release)
		}
	}
}

 

Do you have any other solution? :)

Thanks again,

Axel

Link to comment
Share on other sites

I store when my custom keys are pressed, and loop through that each tick to see if they are unpressed, rather than all the keys.

 

The best way I've found is still using the custom objects, so when I press my 'special attack' key, it activates an instance of special attack or whatever, and that object is then updated each tick (charging up or whatever) and checks as it updates if the custom key is still pressed or not; this way, I'm only checking keys that I know were pressed.

 

You could do the same with a dynamic list of currently pressed keys, or if you made an array for your keybindings it would probably be more economic to simply run through it each tick instead of adding and removing entries to a list, depending on how many keys you have.

Link to comment
Share on other sites

Thanks coolAlias for your useful answer,

on the keyDown event I'll add that key to a list which is looped every tick. For each entry of that list I will check if it is still pressed. If it's has just been released, it will apply the correct code and remove that entry from the list. Seems really fair to me, but dirty :P

 

Thanks again!

Link to comment
Share on other sites

The problem here (imho) is where FML placed the KeyInputEvent hook.

It fires from within the Keyboard.next() loop of Minecraft, but only if getEventKeyState is true, meaning it only fires for key-down events. The hook should be moved down one line, and it could be used properly.

 

Yeah, I was looking at this too last night.  Furthermore the FML KeyInputEvent event parameter passed could have more useful fields.  It seems that polling Keyboard directly and maybe firing custom events (or just processing at the time) is more generally useful, although ideally you'd want to implement the key binding as well to allow user mapping.

 

One question about the Keyboard.next() is: does invoking this method clear the list of key events from Keyboard?  I mean if you call next() do you then have to fully process it or will it still work as expected in addition to anything you might do?

 

Another question: Regarding the "key up" detection, doesn't the getEventKeyState() method do this?  From the comment description for the method, it seems that if Keyboard has a key event where getEventKeyState() returns false that means that the key was released.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

KeyEvent fires for every key event that happens. It is up to you to retrieve the information from the Keyboard class....Indeed it does. If getEventKeyState is false, that means that the current event is a "release-key" event. The problem is: due to the placement of the FML hook, FML's event does not fire in this case, which was my point.

My point about polling and creating own events was about augmenting/replacing the FML hook event.  In other words, by accessing Keyboard class KeyEvent directly you can detect both key down (including repeat it seems) and key up.  So can't you poll and then fire your own events for KeyUp?

 

A question about polling performance.  On the one hand doing something every tick seems like a performance burden, but of course the built-in code is already doing a lot of such polling.  Is the built-in code performance optimized in some way?  It just seems to me that polling, while a bit heavy, is something that computer should still be able to handle fairly well -- just poll KeyBoard.next() and fire off a KeyUp custom event as needed.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Oh. Yeah I misunderstood you. The problem is that the only place to reliably detect key inputs is in a .hasNext() .next() loop. Which MC already has, so you can't really just put in your own.

 

Okay, so I just tried the following:

	@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
public void onEvent(ClientTickEvent event)
{
	while (Keyboard.next())
	{
		System.out.println("Keyboard event detected");
		FMLCommonHandler.instance().fireKeyInput();
	}
}

 

And it "nicely" detects both key down and key up events.  However, it seems to entirely override any other keyboard processing by Minecraft (even ESC no longer works!).  Basically, (like I suspected when asking my first question) that iterating through the Keyboard KeyEvents removes them from the queue and so they no longer get processed by rest of Minecraft. 

 

So the problem with creating your own key up hook is figuring out how to do it without blocking vanilla keyboard processing.  One way might be to put the stuff that is read from the Keyboard.readBuffer back.  There is a readNext() method that is private that is called by the public next() method, and readNext() just reads out the values from the buffer.  You could probably use reflection (right) to access readBuffer and put back what was just read out.  I suspect that would allow vanilla keyboard processing to continue.

 

The other method would be to recreate all of the Minecraft class' keyboard processing but I suspect that would be difficult as it would likely run into issues with private fields/method calls and also potentially would change the sequence of processing in a way that might muck up actual game. 

 

I think I'll play around with using reflection to restore the readBuffer as it seems like a fairly possible way to make the KeyInputEvent hook extend to include key up.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Okay, I'm stuck on the reflection already -- I'm not sure what the instance of the Keyboard  class really is.  It seems that there is no constructor or any call that instantiates it.  Instead it creates its own "implementation" instance of InputImplementation but that is also a private static field within the class.

 

Diesieben07, you're good at reflection -- is there any way to use reflection on this readBuffer field in Keyboard class?  Without an instance to reference I'm not sure how to use reflection...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Oh hell please don't hack into the internals of LWJGL :P You'll most likely cause all sorts of problems, because most of LWJGL is natives.

The right solution is to make a PR to FML to fix the placement of the hook :P

 

yeah, I could tell I was getting into dangerous territory.  I think I'll try polling the keybindings instead and see if I can fire a useful key up event from there.

 

But out of interests' sake: is there a way to do reflection on fields in a non-instantiated class?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

As diesieben07 said, the only way is moving the Forge line in the Minecraft run() method. It's an easy fix, but someone must do it (I'm not good at github at all unluckily).

Jabelar, I tried the same stuff yesterday and I already said your thoughts in my previous posts, like:

 

EDIT: I still don't get how can I detect the key released in a onUpdate function. I tried this, but it will override also all MC keybinds:

 

Obviously, you mustn't use reflections for native code and using the workaroud diesieben07 suggested is good enough, just remember to remove the keybind from the list when it has been released.

Anyway we are all waiting for this little feature and I hope to see it in the next Forge versions :)

 

Have a good day guys!

Axel

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am working on a modpack. When I try to access my server, Minecraft suddenly stops responding for no apparent reason, but I tested the same modpack on my friend's PC and it functions flawlessly there. One thing I have noticed is that the message "[Render thread/WARN] [io.gi.ap.ap.Apoli/]: Received unknown attacker" appears in the log each time my game crashes. My frame rate is high, about 100, and I do not have any mods called Apoli. 9000 MB of the 16000 MB of RAM are allotted to me. Almost everything I could do has been done (updating drivers, removing some mods, lowering the graphic settings, testing singleplayer, etc.) I will leave the moderator list and the latest.log down here.   https://mclo.gs/pYqKP15 (Cannot upload due to file size on pastebin.) Mods: [✔] [1.18.2]-Medieval-Siege-Machines-v1.17 [✔] [1.18.2-forge]-Epic-Knights-8.2 [✔] AchievementOptimizer-1.18.2-1.0.2 [✔] AdvancementPlaques-1.18.2-1.4.5.1 [✔] alexsmobs-1.18.6 [✔] antiqueatlas-7.1.1-forge-mc1.18.2 [✔] Apotheosis-1.18.2-5.8.1 [✔] appleskin-forge-mc1.18.2-2.5.1 [✔] Aquaculture-1.18.2-2.3.12 [✔] aquamirae-5.api10 [✔] archbows-1.0.9-1.18.2 [✔] architectury-4.12.94-forge [✔] armorunder-mc1.18.2-3.0.1 [✔] astikorcarts-1.18.2-1.1.2 [✔] Atlas-Lib-1.18.2-1.1.8 [✔] BetterAdvancements-1.18.2-0.2.0.146 [✔] betteranimalsplus-1.18.2-11.0.10-forge [✔] betterbiomeblend-1.18.2-1.3.5-forge [✔] BetterCompatibilityChecker-1.1.21-build.48+mc1.18.2 [✔] betterfpsdist-1.18.2-1.5 [✔] BetterPingDisplay-1.18.2-1.1 [✔] blockui-1.18.2-0.0.71-ALPHA [✔] BloodAndMadness-Forge1.18.2-v2.1.4 [✔] blueprint-1.18.2-5.5.0 [✔] BOMD-Forge-1.18.2-1.0.6 [✔] Bonfires-1.18.2-1.2.18-2062213 [✔] Bookshelf-Forge-1.18.2-13.3.56 [✔] born_in_chaos_[Forge]1.18.2_1.17 [✔] BrassAmberBattleTowers-1.18.2-2.3.12 [✔] caelus-forge-1.18.1-3.0.0.2 [✔] callablehorses-1.18.2-1.2.2.5 [✔] cameraoverhaul-1.0-1.18.2 [✔] carrotslib-mc1.18.2-4.0 [✔] catalogue-1.6.2-1.18.2 [✔] champions-forge-1.18.2-2.1.6.3 [✔] citadel-1.11.3-1.18.2 [✔] cloth-config-6.5.116-forge [✔] ColdSweat-2.3-b04f [✔] collective-1.18.2-7.7 [✔] configured-2.0.1-1.18.2 [✔] connectedness-1.18.2-2.0.1a [✔] Controlling-forge-1.18.2-9.0+23 [✔] corpse-1.18.2-1.0.2 [✔] CosmeticArmorReworked-1.18.2-v2a [✔] create-1.18.2-0.5.1.f [✔] Croptopia-1.18.2-FORGE-2.1.0 [✔] ctov-2.9.4 [✔] CullLessLeaves-Reforged-1.18.2-1.0.5 [✔] curios-forge-1.18.2-5.0.9.2 [✔] DarkerSouls1.18.2Forgev1.3.1 [✔] Divine Weaponry v1.4 1.18.2 [✔] DoggyTalentsNext-1.18.2-1.18.18 [✔] domum_ornamentum-1.18.2-1.0.77-ALPHA-universal [✔] dragonmagicandrelics-1.18.2-2.1.38 [✔] dragonseeker-1.1.1-1.18.2 [✔] dungeons_enhanced-1.18.2-3.2.1 [✔] DungeonsArise-1.18.2-2.1.52-release [✔] dungeonsweaponry-1.12.2-1.18.2 [✔] EFMCompat 18.2.0 [✔] EnchantmentDescriptions-Forge-1.18.2-10.0.17 [✔] EnigmaticLegacy-2.25.0 [✔] Entity_Collision_FPS_Fix-forge-1.18.2-1.0.0 [✔] entity_model_features_forge_1.18.2-2.0.2 [✔] entity_texture_features_forge_1.18.2-6.0.1 [✔] entityculling-forge-1.6.1-mc1.18.2 [✔] epic_fight_battle_styles-1.0.2 [✔] EpicFight-18.5.24 [✔] falchionmoveset-18.5.0 [✔] FarmersDelight-1.18.2-1.2.3 [✔] Fastload-Reforged-mc1.18.2-3.4.0 [✔] ferritecore-4.2.2-forge [✔] finsandtails-1.18.2-1.1.1 [✔] fish_of_thieves-mc1.18.2-v1.1.1-forge [✔] Forge 1.18.2 Minecraft Middle Ages 0.0.4 [✔] francium-1.18.2-1.0.1 [✔] ftb-essentials-1802.2.2-build.83 [✔] ftb-library-forge-1802.3.11-build.177 [✔] geckolib-forge-1.18-3.0.57 [✔] getittogetherdrops-forge-1.18.2-1.3 [✔] Gobber2-Forge-1.18.2-2.6.37 [✔] goblintraders-1.8.0-1.18.2 [✔] goodall-1.0.2-forge [✔] gpumemleakfix-1.18.2-1.6 [✔] guardvillagers-1.18.2.1.4.4 [✔] healingcampfire-1.18.2-5.2 [✔] hexerei-0.2.2 [✔] horsecombatcontrols-1.18.2-1.0.1 [✔] iceandfire-2.1.13-1.18.2-beta-3 [✔] Iceberg-1.18.2-forge-1.0.49 [✔] ImmediatelyFastReforged-1.18.2-1.1.10 [✔] immersive_melodies-0.1.2+1.18.2-forge [✔] ironchest-1.18.2-13.2.11 [✔] ironfurnaces-1.18.2-3.3.3 [✔] irons_spellbooks-1.18.2-1.1.4.5 [✔] ItemBorders-1.18.1-1.1.5 [✔] ItShallNotTick-1.0.22-build.34 [✔] Jade-1.18.2-forge-5.3.2 [✔] JadeAddons-1.18.2-forge-2.5.0 [✔] jecalculation-forge-1.18.2-4.0.4 [✔] jeed-1.18.2-1.11 [✔] jei-1.18.2-forge-10.2.1.1008 [✔] jepp-1.18-1.0.0 [✔] JustEnoughAdvancements-1.18.2-3.2.0 [✔] justenoughbreeding-forge-1.18.2-1.2.1 [✔] JustEnoughProfessions-1.18.2-1.3.0 [✔] JustEnoughResources-1.18.2-0.14.2.206 [✔] Kobolds-2.9.3-1.18.2-final [✔] kotlinforforge-3.12.0-all [✔] L_Enders Cataclysm-0.51-changed Them -1.18.2 [✔] lazydfu-1.0-1.18+ [✔] leaky-1.18.2-1.3 [✔] LegendaryTooltips-1.18.2-1.3.1 [✔] letmedespawn-1.18.x-1.19.x-forge-1.0.3 [✔] LibX-1.18.2-3.2.19 [✔] lightspeed-1.18.2-1.0.5 [✔] lodestone-1.18.2-1.4.2 [✔] loot_journal-1.1.0-1.18 [✔] lootr-forge-1.18.2-0.3.29.71 [✔] malum-1.18.2-1.5.0.1 [✔] medieval_paintings-1.18.2-7.0 [✔] memoryleakfix-forge-1.17+-1.1.2 [✔] MerchantMarkers-1.18.2-forge-1.3.1 [✔] minecolonies-1.18.2-1.1.148-BETA [✔] MmmMmmMmmMmm-1.18.2-1.5.2 [✔] mna-2.0.0.33 [✔] modernfix-forge-5.18.0+mc1.18.2 [✔] modonomicon-1.18.2-1.33.1 [✔] mowziesmobs-1.6.3 [✔] multi-piston-1.18.2-1.2.15-ALPHA [✔] mythicmounts-18.2-7.2-forge [✔] NefsFarmDecoExpGear+v0.3(1.18.2) [✔] nutritionalbalance-1.18.2-3.2.4 [✔] obscure_api-10 [✔] occultism-1.18.2-1.84.0 [✔] octolib-1.18.2-0.3 [✔] oculus-mc1.18.2-1.6.4 [✔] OfflineSkins-1.18.2-v2 [✔] Orcz_0.83_1.18.2 [AI FIXES] [✔] origins-classes-forge-1.1.6 [✔] origins-forge-1.18.2-1.5.0.2-all [✔] Paraglider-1.18.2-1.6.0.6 [✔] Patchouli-1.18.2-71.1 [✔] physics-mod-pro-v153-forge-1.18.2 [✔] Placebo-1.18.2-6.6.7 [✔] player-animation-lib-forge-1.0.2+1.18 [✔] pluto-mc1.18.2-0.0.6 [✔] polymorph-forge-1.18.2-0.50 [✔] Prism-1.18.2-1.0.1 [✔] projectvibrantjourneys-1.18.2-4.1.1 [✔] puffish_attributes-0.5.0-1.18.2-forge [✔] puffish_skills-0.13.1-1.18.2-forge [✔] realmrpg_imps_and_demons_0.9.0_forge_1.18.2 [✔] recruits-1.18.2-1.11.4.2 [✔] reforgium-1.18.2-1.0.12a [✔] Reldas+Medieval+Armor+1.18.2(2.0) [✔] relics-1.18.2-0.6.2.4 [✔] reputation-1.18.2-1.0.0 [✔] RPG_style_more_weapons_4.9.6R [✔] Rrls-1.18.2-3.1.0-forge [✔] rubidium-0.5.6 [✔] rubidium-extra-0.4.18+mc1.18.2-build.86 [✔] saturn-mc1.18.2-0.1.5 [✔] savage_and_ravage-1.18.2-4.0.1 [✔] seaborgium-mc1.18.2-0.1.3b [✔] SereneSeasons-1.18.2-7.0.0.15 [✔] ShoulderSurfing-Forge-1.18.2-4.2.1 [✔] SimpleLogin-1.18.2-1.0.0 [✔] simplyswords-forge-1.40-1.18.2 [✔] SleepingOverhaul-2.0.2-Forge-1.18.2 [✔] smallships-forge-1.18.2-2.0.0-b1.3.1 [✔] SmartBrainLib-forge-1.18.2-1.9 [✔] smoothboot(reloaded)-mc1.18.2-0.0.4 [✔] sophisticatedbackpacks-1.18.2-3.20.2.1036 [✔] sophisticatedcore-1.18.2-0.6.4.604 [✔] soul_like_armors_reworked_1.18.2_(v138) [✔] SpartanShields-1.18.2-3.0.1 [✔] SpartanWeaponry-1.18.2-3.0.5-all [✔] SpitItOut-1.18.2-1.0.1 [✔] starterkit-1.18.2-5.3 [✔] Structory-1.18.2-1.0.2 [✔] structure_gel-1.18.2-2.4.8 [✔] structurize-1.18.2-1.0.424-ALPHA [✔] swem-forge-1.18.2-1.4.6 [✔] takesapillage-1.0.3-1.18.2 [✔] TerraForged-1.18.2-0.3.1-alpha-2 [✔] TextruesRubidiumOptions-1.0.8-mc1.18.2 [✔] theimpossiblelibrary-1.18.2-0.3.1 [✔] torchmaster-18.2.1 [✔] Totemic-forge-1.18.2-0.12.9.1 [✔] tov-0.1(ALPHA) [✔] towns_and_towers_forge-1.10.0.1+1.18.2 [✔] TravelersTitles-1.18.2-Forge-2.1.1 [✔] unoriginal-datapack [✔] untamedwilds-1.18.2-2.4.3 [✔] upgrade_aquatic-1.18.2-4.0.0 [✔] valhelsia_core-forge-1.18.2-0.4.0 [✔] valhelsia_structures-1.18.2-0.1.1 [✔] Vampirism-1.18.2-1.8.8 [✔] WeaponsOfMiracles-18.1.7.38 [✔] Werewolves-1.18.2-1.0.0.1 [✔] workers-1.18.2-1.7.8 [✔] YouDied-1.18.1-1.0.2.9 [✔] YungsApi-1.18.2-Forge-2.2.9 [✔] YungsBetterMineshafts-1.18.2-Forge-2.2 [✔] YungsBetterStrongholds-1.18.2-Forge-2.1.1 [✔] zoomerzoom-1.4
    • Turn casual moments into unforgettable dates – choose the best casual dating site! Sexy Womans from your town
    • I am trying to make a Modpack for me and my friend and the buddycard expansion mod keeps crashing is there anything I can do, I'll copy and paste the crash message and the attack a link to the crash log below: Buddycards Expainsion (buddycardsexp) encountered an error during the common_setup event phase java.lang.NoSuchMethodError: 'void com.wildcard.buddycards.item.BuddycardBinderItem.<init>(com.wildcard.buddycards.registries.BuddycardsItems$BuddycardRequirement, net.minedcraft.world.item.Item$Properties, com.wildcard.buddycards.core.BuddycardSet)'   https://drive.google.com/file/d/1XlEYKVi2uUyS_UxRKByqCKlfnVswvHbb/view?usp=sharing
    • So, my friend and I are playing modpack on version 1.20.1. Sometimes, when a friend dies, he crashes from the server and cant rejoin anymore, so I have to load backup of the world. After loading the backup, everything goes well, but then sometimes it crashes again after death. Here is lof file: https://pastebin.com/KaZTmvhc The lines where the friend crashes start at line 2471 
    • The Minecraft launcher is now "preparing" the game but is taking forever. Any suggestions?
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.